| Author |
Garbage Collection
|
Gagan Sabharwal
Ranch Hand
Joined: Apr 23, 2006
Posts: 48
|
|
SCJP(K&B)question 2,Self Test, chapter 3 -Assignments How many objects are elligible for garbage collection? 1)1 2)2 The answer is the second option.But i am unable to understand the reason for it.
|
 |
Praveen Seluka
Ranch Hand
Joined: Jul 17, 2007
Posts: 95
|
|
Hi gagan I think two objects are eligible for garbage collection as 1.c1 is made null.It doesnt point to anything. 2.c3 is also null,since c1.go(c2) returns null. Is that right people,somebody explain this fully Thanks Praveen SP
|
 |
Akhilesh Trivedi
Ranch Hand
Joined: Jun 22, 2005
Posts: 1493
|
|
Originally posted by Gagan Sabharwal: SCJP(K&B)question 2,Self Test, chapter 3 -Assignments How many objects are elligible for garbage collection? 1)1 2)2 The answer is the second option.But i am unable to understand the reason for it.
At line 1 one object is created (let me call it obj1) so c1---> obj1 similarly at line 2 c2-->obj2 Number of CardBoard objects created after execution of line 2 = 2. At line 3 1. There is only one reference, c3. No object. 2. We are passing the value of c2's reference to method local cb at line 4. i.e cb also refers obj2 so on total we have two objects and three reference, c1--> obj2 c2-->obj2 cb-->obj2 further, c1 and c2 are local to main() and cb is local to go(). At line 5, cb-->null c1--> obj2 c2-->obj2 cb-->null Remember c2 is still pointing to obj2. cb which is null is returned and assigned to c3. At line 3 1. cb no more exists, as it is local to only go(). 2. c3-->null At this point c1-->obj1 c2-->obj2 c3-->null line 7, c1 = null. i.e. c1--> null, so obj1 is freed and is not being pointed by anything, victim of garbage collection? yes. But just one small thing with obj1, it has an internal 'story' variable which is a wrapper object of type 'Short' (note the capital 'S'). c1 and story are two objects to be wrapped up.! !!  [ August 07, 2007: Message edited by: Akhilesh Trivedi ]
|
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
|
 |
Gagan Sabharwal
Ranch Hand
Joined: Apr 23, 2006
Posts: 48
|
|
Thanks for replying. That was the same explnation which i had in my mind before looking into the solution.If you look at the solution given in the book ,it is something like this. Option second is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
|
 |
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
|
|
Hi Gagan, please have a look at the K & B Errata. In your example there is only one object eligible for garbage collection. The corrected example:
|
 |
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
|
|
Hi Akhilesh, very good explanation for the corrected example. But based on the given code, it contains one error. Will you find it?
|
 |
Akhilesh Trivedi
Ranch Hand
Joined: Jun 22, 2005
Posts: 1493
|
|
Hi Manfred, i thought i am all ok, but now that you have indicated the possibility of an error.... you make me think buddy. There are two things running in my head, one is Do I need to treat this as literal? and second is, if the object is garbage collected then do the reference it holds are also eligible for GC or not? Else there is no other direction my mind can wander about.... :roll:
|
 |
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
Hi Manfred, Why in that code there is only one object eligible for gc? I'm confused now. Can you explain? Thanks,
|
 |
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
|
|
Hi, have a look at 5.1.7 Boxing Conversion in the Java Language Specification and try the following:
|
 |
Sahid Khan
Ranch Hand
Joined: Jun 27, 2007
Posts: 41
|
|
Hi Manfred, Yes, i3 and i4 comparison will be false. So if I understand it correctly, i3 and i4 point to different objects. So if you make them null those Integer objects will be eligible for garbage collection. So in that case, Akhilesh's arguments remain true, assuming the corrected example i.e., story=200. Am I missing something? [ August 09, 2007: Message edited by: Sahid Khan ]
|
 |
Praveen Seluka
Ranch Hand
Joined: Jul 17, 2007
Posts: 95
|
|
Hi all I have been reading about wrapper classes. I have a doubt. Integer i=new Integer(2);line 1 Integer j=Integer.valueOf(2);line 2 Integer i=2;line 3 Line 1 - It creates a new Integer object Line 2 - The method returns a newly created integer object Line 3 - what does this line do.Object creation? Help me in understanding this. Thanks Praveen Sp
|
 |
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
|
|
Originally posted by Sahid Khan: Akhilesh's arguments remain true, assuming the corrected example i.e., story=200.
As I said, it's a very good explanation for the corrected example.
|
 |
Sahid Khan
Ranch Hand
Joined: Jun 27, 2007
Posts: 41
|
|
oh! ok I thought you said otherwise. any way no issues. thanks for all these thoughtful posts.
|
 |
Sahid Khan
Ranch Hand
Joined: Jun 27, 2007
Posts: 41
|
|
Line 3 - what does this line do.Object creation?
Yes this line also does create an object. But jvm caches it i.e., some where in your program, if jvm needs to box a primitive int (value 2) again, it will not create a new one. But it will use the same one which has been created in this line. JVM caches this value if it is between -128 and 127, i.e., it can be put in a byte. HTH.
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Thats good. An earlier discussion related to the same post is here .
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Akhilesh Trivedi
Ranch Hand
Joined: Jun 22, 2005
Posts: 1493
|
|
Thanks Manfred ! And thanks Raghavan for sharing the link.
|
 |
 |
|
|
subject: Garbage Collection
|
|
|