• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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 !
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic