• 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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a question in K&B book of chapter-3 Self-Test.


When //dostuff is reached , how many objects are eligible got GC?

As per Book answer it says 2 are eligible.(C1 and its associated Short story object.)
I think C2 and C3 and their associated objects are also eligible as their reference are null at this point.
Can any body explain me how?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tarun,

There is an important omission in your code:

The reason why there is only 2 objects elligible for GC is that c2 still points to the original object. When you pass c2 to go(), the reference value is copied, so any assignments (including assigning null) to the parameter in the method don't affect the original reference. Java uses value-copy semantics both for reference and primitive variables.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, please search this forum for this question -- this question comes up often here.

Second, also check the errata. There is a error in that the Short isn't eligible for GC -- autoboxing uses a cache, and a Short with a value of 5 is gotten from the cache.

Henry
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't realize that, but you are right. Where is this errata that you speak of? I could only find an errata for the previous edition (SCJP 5.)

Thanks.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Errata is here :

259....clarify...Q-2: code s/b: Short story = 200;
& 266

This problem is only in SCJP 5 book, in SCJP 6 book it is Short story=200; already, so Short s is not taken from cache as 200>127, it builds on heap now, so for C1 object, it's Short s is also eligible for GC.


 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right in that it is corrected in the new edition, Punit.

I was aware of that errata for the SCJP 5 Guide. Is there any other thread for SCJP 6? If not, how do we go about reporting errata? Do we contact Bert directly?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
Tarun Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben and all who post their reply.
And i will take care of code tag .
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic