简体   繁体   中英

Garbage collection eligible objects

I have 6 separate java Classes A,B,C,D,E and F .

Class A creates object of Class B and calls a method in Class B .

Class B method creates an object of class C and calls a method in class C .

Class C method creates an objects of D,E,F classes and calls their methods.

when finally control comes back to class A at the end , if I make the object reference of Class B as null, Will this make all the objects of class B,C,D,E,F created so far eligible for garbage collection?

Yes. It will. Since the parent class is now referenced to null and is orphan, all reference objects below it will be eligible for GC.

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

http:// 192.9.162.55 /docs/books/performance/1st_edition/html/JPAppGC.fm.html

是的,如果它们是在方法内部创建的,那么它们将有资格进行垃圾回收,即在本地创建则是合格的,并且如果它们是实例变量对象,则只有将该对象的副本设置为null才有资格使用gc。

Yes. "An object is eligible for garbage collection when there are no more references to that object."

So if b is no longer referenced, it is eligible for gc. When in turn it is collected, there are no more references for c, so c becomes eligible, and so the story goes on.

GENERALLY yes, but without more information we can't say for certain.
Prime example would be if say C places a reference to the instances of D it creates in a Collection which is stored in the http session or EJB transaction (or stores a reference there directly). If that happen, those references would prevent the relevant instances of D from being garbage collected, and everything they keep references to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM