• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

have a look at 5.1.7 Boxing Conversion in the Java Language Specification and try the following:
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats good.

An earlier discussion related to the same post is here .
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manfred !
And thanks Raghavan for sharing the link.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic