This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Garbage Collection

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
  • 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: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Ruben and all who post their reply.
And i will take care of code tag .
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic