• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Garbage Collection Help

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below question is from K&B chapter 3 ,2nd question.
It asks for how many objects are eligible for garbage collection after the \\doStuff line.

The correct answer is C.
As explanation given in K&B is like this
"Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.".

But as per my thinking c1 and c2 will be garbaged collected.
I didn't understand the explanation of K&B .

class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Regards,
Hamraj
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

c2 is not eligible for garbage collection as it is still pointing to the object created through:


The statement:



does result in cb(copy of c2) pointing to null but c2 is still pointing to an object.
 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for my typo.
My answer was c1 and c3 will be eligible for garbage collection.
Since when go method is called,it returns null. which is referenced by c3.
so c1 and c3 should be correct.

But what does this mean that(as per K&B explanation) "C is correct.Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible."
Please help me for this doubt.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But c3 never references an object, because it is assigned null at the point of declaration. So there is no object associated with c3 that can be Garbage Collected.

Each instance of CardBoard HAS-A Short wrapper object. So if an instance of CardBoard is eligible for GC, then we might expect its instance of Short to also be eligible. However, because the Short is assigned a value using autoboxing and that value is within the range of a byte, there is a slight glitch in this reasoning. In the K&B errata, this has been changed so that the value is outside the range of a byte (200 instead of 5).

For more discussion, see this thread.
 
Get off me! Here, read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic