0

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?

Vegard
  • 4,802
  • 1
  • 20
  • 30
user1929905
  • 389
  • 3
  • 9
  • 25
  • I guess you mean all the objects in the mentioned graph. Not all objects of the given classes. If objects of the mentioned types have been constructed elsewhere they will not be GC'ed – Rune FS Mar 15 '13 at 12:56

4 Answers4

0

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

Snake Eye
  • 535
  • 3
  • 13
  • Check the following link as well : http://stackoverflow.com/questions/2433261/when-and-how-are-classes-garbage-collected-in-java – Snake Eye Mar 07 '13 at 06:21
0

Yes , they will be eligible for garbage collection provided one condition if they are created inside the methods i.e locally then they are eligible and also if they are instance variable objects then only copy of that object which is set to null will be eligible for gc.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • The Object reference for class B is a single local object.will this make all objects of class C,D,E,F eligible for gc? – user1929905 Mar 07 '13 at 06:37
  • No,you should know the basic.For every object, All instance variables have different copy.So if you are assigning null to one object of B, then only copies inside that object(which are C,D,E,F objects) will be garbage collected. – Android Killer Mar 07 '13 at 06:45
  • Search these basic things and u will get a lot of things to read. – Android Killer Mar 07 '13 at 06:45
0

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.

tbsalling
  • 4,477
  • 4
  • 30
  • 51
0

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.

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • My classes uses collections but does not use httpsession or ejb related stuff.will they be eligible now? – user1929905 Mar 07 '13 at 06:57
  • @user1929905 that depends. As long as a reference exists in a collection and that collection is not itself eligible for garbage collection the instance that reference points to isn't eligible either. So if you had a collection in the class calling A that was passed into a method on A which added a reference to B to that collection B would remain as long as it remained in that collection unless the collection itself was removed. – jwenting Mar 07 '13 at 07:48