Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 4
    The JVM does not do reference counting. Strictly speaking, the spec does not mandate how (or even that) garbage collection is implemented, but all mainstream JVMs use some sort of tracing collector. Commented Aug 14, 2012 at 9:20
  • @MichaelBorgwardt: changed my answer due to your comment. Commented Aug 14, 2012 at 9:57
  • if between step 2 and step 3 the reference to the first instantiation is stored somewhere else (like Test test2 = test;), and that variable is still in scope when step 3 occurs... This is somewhat pedantic but garbage collection isn't based on scope, but on reachability. It's possible that a variable is in scope, but its value is no longer used, and that value can be garbage collected. Garbage collectors don't have scopes handy to make decisions based on that. Commented Jan 3, 2017 at 19:04
  • @Doval: I agree, this comment is pedantic ;-) Commented Jan 3, 2017 at 19:25