Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Garbage Collection

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

I have some difficulty understanding the answer of the first question from chapter 3 of the Kathy Seira SCJP 6 book. The question, given answer and my doubt are as follow:

Question:


Given:

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


Given answer:


C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.


and my doubt is, how did the Short wrapper object, become an 'associated' object?? Hope someone can help. Thanks.

regards
John
 
Saloon Keeper
Posts: 13383
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the Cardboard object holds on to a Short instance, right? So when the Cardboard instance is eligible, so is the Short instance, because there are no other variables that hold a reference to the Short object.
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object is eligible for garbage collection when there are no more references to that object. The way I would solve these questions is to dry run. Just by looking at the code c3 & c1 will have NULL. Please check out Oracle tutorial.
 
author
Posts: 23909
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
Please SearchFirst. This question has been asked many times in the past. In fact, a quick search yield about three pages of hits. The first three hits on the first page (not counting this topic) were...

https://coderanch.com/t/568723/java-programmer-SCJP/certification/Garbage-Collection
https://coderanch.com/t/561000/java-programmer-SCJP/certification/Garbage-collector
https://coderanch.com/t/561429/java-programmer-SCJP/certification/possible-glitch-garbage-collector-example

Henry
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what is the reason for that c3 is not eligible for GC?
 
Hasitha Su
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hasitha Su wrote:But what is the reason for that c3 is not eligible for GC?

 
Ranch Hand
Posts: 31
Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Post Today 9:57:27 AM Subject: Garbage Collection
But what is the reason for that c3 is not eligible for GC?

\

I believe you are asking why c3 is not eligible for garbage.
Reason: Because c3 has no reference to existing object and also have not created any instance of new object. So there nothing present for Garbage collection.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terminology is EXTREMELY IMPORTANT. Strictly speaking, c3 is a reference variable. Reference variables are NEVER eligible for garbage collection. As a reference variable, c3 might be referring to an object. At some point, when that object has no "live" reference variables pointing to it, that object can become eligible, but reference variables are never eligible.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here two objects are eligible for Garbage collection. First of all we are explicitly assigning null to the C1. That's why c1 is eligible for Garbage Collection. Then C3 is assigned by calling method on C1. As c1 is null, c3 is also eligible for garbage collection. So totally 2 objects are eligible for garbage collection.


Thank you.
 
Bert Bates
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you MUST come up with a concrete way to refer to objects. given that references to objects can be re-assigned, you have to say things like:

"the object referred to by var c1 at line x"

it's NOT sufficiently precise to say "c1 is eligible for GC".

c1 is a reference var, it's NOT an object. as the code runs, c1 might start off by referring to one object, and then later on it might be changed to refer to a different object.

does that help?
 
Greenhorn
Posts: 8
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When ever i took any practise scjp test, i got vey low score in garbage collection. After reading this post, i am lot more clear on the idea of GC.

Thanks a lot Bert Bates & Kathy Sierra. I am reading your book on SCJP 1.6

Its little more fun here : http://radio.javaranch.com/corey/2004/03/25/1080237422000.html


Sincerely
Rahul Parakkat
 
You showed up just in time for the waffles! And this tiny 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
reply
    Bookmark Topic Watch Topic
  • New Topic