• 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

K&B : Garbage Collection Question..doubt!!

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers...

I need help with K&B page 259, Question 2.
The solution doesn't give me much insight.

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
}
}
Question : when //do stuff is reached, how many objects are eligible for GC??

My doubt: object c3 gets null assigned after call to c1.go(c2), so why not c1 and c3 both are eligible for GC??

please help....
[ May 28, 2006: Message edited by: sab son ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this thread
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple answer is>>> c3 was not pointing to any object before call to c1.go(c2). So setting it to null by method call will not hav ny effect on gc.

So only one CardBoard object is eligible for garbage collection. Since it has a included Short object, so total object eligilbe for garbage collection will be 2.( One CardBoard object + included Short object).

regards

Naseem
[ May 28, 2006: Message edited by: Naseem Khan ]
 
sab son
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wise and Naseem, thanks, help is appreciated !
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic