• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubt in Garbage Collection (GC) question in K&B Book.

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

I'm having difficulty in the answer by K&B Book, for the question below. Given the code below.
On running the code, how many objects are eligible for Garbarge Collection when line 'do complex stuff' is reached.


class TestGC {
Short s = 200;

public static void main(String args[]) {

TestGC t1 = new TestGC();
TestGC t2 = new TestGC();
TestGC t3 = t1.go(t2);

// do complex stuff
}

TestGC go(TestGC t) {

TestGC x = t
x = null;

return x;
}

} // end class

What is the correct answer

A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
G. No possible to determine objects that are eligible for GC.

My answer was 'and is still' A. (0) because when this line 'do complex stuff' is reached the object references t1, t2, and the reference instance variables t1.s, t2.s are still referrering to objects on the Heap (which are 4 of them 2 TestGC objects and 2 Wrapper objects of type Short).

But the book says the correct answer is C. (2); two objects that is one referred to by t2 and the wrapper object (Short) referred to by reference instance variable s.

Can you please clarify me on the above? Thanks in advance.

Regards,
Siphiwe Madi
SCJP 5 (preparing)
[ May 13, 2008: Message edited by: Siphiwe Madi ]
 
Ranch Hand
Posts: 35
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you..There are no objects available for GC at that point
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

This question has been discussed a lot

There's good news and bad news:

The good news is that you can find thorough discussions of this question in the archives of this forum.

The bad news is that you got a bad version of the original question. The code that you posted is VERY different than the code in the book!

hth,

Bert
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer in the book is correct by referring the following text from the book:

There is another way in which objects can become eligible for garbage collection, even if they still have valid references! We call this scenario "islands of isolation."
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The bad news is that you got a bad version of the original question. The code that you posted is VERY different than the code in the book!



Apologies , when i posted this question I didn't have the Book with me. I was recalling it from my Head and wanted clarification as the books answer bothered me.

And I agree, the answer in the book is correct. I had missed 1 important line before 'do complex stuff' which is:

t1 = null;

which causes 2 objects of type (TestGC and Short) to be eligible for Garbage Collection.

Thanks a lot 4 your assistance.

Regards,
Siphiwe Madi
SCJP 5 (preparing)
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no worries
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic