aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help with Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help with Garbage Collection" Watch "Help with Garbage Collection" New topic
Author

Help with Garbage Collection

Ahmad Hamdan
Greenhorn

Joined: Aug 04, 2008
Posts: 4
There is this question in chapter 3 of scjp 6 study guide book

The question is:
which are true about objects created in the main() and eligible for Garbage Collection:

class Dozens{
int[] dz={1,2,3,4,5,6,7,8,9,10,11,12};
}
public class Eggs{
public static void main(String[] args){
Dozens[] da= new Dozens[3];
da[0]=new Dozens();
Dozens d=new Dozens();
da[1]=d;
d=null;
da[1]=null;
//line 14
}
}


In the book the answers are five objects are created and this i understand and the other answer is two objects are eligible for the GC
In the explanation it says the two objects are da[1] and it array which is dz.

what i don't understand is why isn't Object 'd' included in the GC because i see it referenced to null
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
d is not an object. it is a reference variable.
da[1] is refering to the same object as d.
when d = null; da[1] still is referencing the object.
when da[1] = null then the object don't have more references.
dz still has a reference, but it is isolated, then it can be garbage collected.

well ... I'm not good to explain xD
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452




please post within the code tag to make it a bit more readable.


SCJP 5.0 SCWCD 5.0
Ahmad Hamdan
Greenhorn

Joined: Aug 04, 2008
Posts: 4
Leonardo i still don't understand
i know 'd' is a refernce variable but its refering to null so why isn't it eligible for the garbage collection?

and thanks avi for the advice
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

at first an array object da lets say o1
da[0]--------->dozen obj+ array obj 2 objects lets say o2 & o3
d ------------->dozen obj +array obj 2 object lets say 04 & o5
da[1] -----------------^ no new objects
d=null no object is eligible
da1[1] = null o4 & o5 are eligible

hence

five objects were created
at line 14 two are eligible for gc

solve the problem by creating a heap i can't show you the one i created for it
Ahmad Hamdan
Greenhorn

Joined: Aug 04, 2008
Posts: 4
Thanks Avi i understand now
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

you 're welcome buddy
my suggestion : always try to solve gc problems using a heap and always post codes within code tags
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

this is the heap which i created



[Thumbnail for Noname.jpg]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Garbage Collection
 
Similar Threads
Garbage Collector Question-1
Question about GC and Objects
Garbage Collection - P274 Kathy Sierra
Garbage Colection
HI please Help with. GC Kathy and Bert Book