• 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 Problem

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 4(Chisholm’s GC test)

Which object is not eligible for garbage collection after method m1 returns?
a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above
Answer:
g) None of the above
1)I want to know those instances of the class that are eligible for the garbage collection (mine answer is all the four).
2)And also if u know of any good stuff on the gc that is available on the net then do tell me(I did badly in this topic.I am still crying hunnnnn

3)Also why this big headache is a topic in SCJP(don’t answer this….
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harvinder,


1)I want to know those instances of the class that are eligible for the garbage collection (mine answer is all the four).


I think you answered correctly.
The four objects created in the first two lines of method m1() are all eligible for the garbage collection after method m1 returns, because only m1's local variables: i1, i2, i3, i4 refer to those four objects. After method m1 returns, those local variables goes out of scope and no more references to the four objects, so they are all eligible for GC.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And of course the stated answer: 'none of the above' is quite correct!
This is a trick question of sorts, to test if you really read the question well before answering.
The question states specifically "Which object is not eligible for garbage collection after method m1 returns? "
As ALL objects are eligible, NONE are NOT eligible.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Hadiprijanto:

The four objects created in the first two lines of method m1() are all eligible for the garbage collection after method m1 returns, because only m1's local variables: i1, i2, i3, i4 refer to those four objects. After method m1 returns, those local variables goes out of scope and no more references to the four objects, so they are all eligible for GC.


Examining the question a little more to make sure I'm getting this whole garbage collection thing...
There are also four additional objects created in lines 3 & 4 of m1() that also become eligible for garbage collection. Even though the references to those objects (the ones created in lines 3 & 4) never go away, they become eligible because the references to the objects they are contained in go away, so no existing thread has access to them. Is this correct, or am I off here?
 
David Hadiprijanto
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,


There are also four additional objects created in lines 3 & 4 of m1() that also become eligible for garbage collection.


There are no more objects created in lines 3 & 4 of m1(). Perhaps, you mean there are four other references made to the four objects in lines 3 & 4 of m1()?


Even though the references to those objects (the ones created in lines 3 & 4) never go away, they become eligible because the references to the objects they are contained in go away, so no existing thread has access to them. Is this correct, or am I off here?


The keyword here, no live thread has access to them. So, you are correct that even though there are still references to the four objects other than i1, i2, i3, and i4, (as a result of lines 3 & 4 of m1()), but because they merely points to each other/itself, and no other live thread can access them, they become eligible for GC.
Hope this helps.
[ February 20, 2004: Message edited by: David Hadiprijanto ]
 
Dave Wolf
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Hadiprijanto:
[QB]
There are no more objects created in lines 3 & 4 of m1(). Perhaps, you mean there are four other references made to the four objects in lines 3 & 4 of m1()?
[QB]


Ummmm... yeah, yeah. That's exactly what I meant to say.
Thank you for the clarification.
reply
    Bookmark Topic Watch Topic
  • New Topic