开发者

In Java, when does an object become unreachable?

开发者 https://www.devze.com 2023-02-24 15:08 出处:网络
In Java, what is an unrea开发者_Go百科chable object? When does the object become unreachable? While studying garbage collection I was not able to understand this concept.

In Java, what is an unrea开发者_Go百科chable 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?


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.

class A { int i = 5; }
class B { A a = new A(); }
class C {
   B b;
   public static void main(String args[]) {
      C c = new C();
      c.b = new B();
      // instance of A, B, and C created
      c.b = null;
      // instance of B and A eligible to be garbage collected.
   }

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.

Integer i = new Integer(4);
// the new Integer object is reachable  via the reference in 'i' 
i = null;
// the Integer object is no longer reachable. 


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


An unreachable object, is an object that doesn't have a "reachable" reference to it. In other words, no references to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消