In java, what is an unreachable object? When does the object become unreachable? While studying garbage collection i was not able to understand this concept.
Can anyone give any ideas with examples?
In java, what is an unreachable object? When does the object become unreachable? While studying garbage collection i was not able to understand this concept.
Can anyone give any ideas with examples?
in the object graph when one is not linked to it, then the reference become unreachable. then then the garbage collector scans for these dangling orphan objects and swipe out regaining allocated memory.
java.lang.ref.{Phantom,Soft,Weak} Reference Enqueues unreachable objects. If the finalizer is run, the GC already knows its unreachable.
read about finalization- http://java.sun.com/developer/technicalArticles/javase/finalization/
In a language which has only strong references, heap objects can either be reachable or unreachable from the program. The set of reachable objects is determined by the set of class variables and method variables in the program pointing to heap objects. This set is usually referred to as the root set of the program. An object pointed by a variable in the root set of the program is reachable. In addition, an object might be indirectly reachable. That is, an object is reachable if there is another reachable object pointing to it. Such chain of references from the root set of the program to a heap object is called reachability path. An object may have more than one reachability path to it as well as have no reachability paths at all. If the object has no reachability paths it is deemed garbage and can be immediately collected by the garbage collector.
An object enters an unreachable state when no more strong references to it exist. When an object is unreachable, it is a candidate for collection. Note the wording: Just because an object is a candidate for collection doesn't mean it will be immediately collected. The JVM is free to delay collection until there is an immediate need for the memory being consumed by the object. It's important to note that not just any strong reference will hold an object in memory. These must be references that chain from a garbage collection root. GC roots are a special class of variable that includes
Temporary variables on the stack (of any thread) Static variables (from any class) Special references from JNI native code
more if you like to http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html
When there are no longer any reference variables referring to it, OR when it is orphaned in an island.
An island being an object that has a reference variable pointing to it, however that object has no reference variables pointing to it.
EDIT: Just want to point out that even though the instance of A has a reference, it is on an island now because the instance of B does not have a reference to it. The A instance is eligible for garbage collection.
An object is unrechable when there are no more references to it, or those references are themselves from unrechable objects.
An unreachable object, is an object that doesn't have a "reachable" reference to it. In other words, no references to it.