This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Gagan Sabharwal
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gagan Sabharwal
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

have a look at 5.1.7 Boxing Conversion in the Java Language Specification and try the following:
 
Sahid Khan
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh! ok I thought you said otherwise. any way no issues. thanks for all these thoughtful posts.
 
Sahid Khan
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats good.

An earlier discussion related to the same post is here .
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manfred !
And thanks Raghavan for sharing the link.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic