• 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

Question on Garbage Collection

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code: After what line the MyClass object created at line 1 will be eligible for GC?



The answer says: At line 6, x starts pointing to a new MyClassObject and there remains no reference to the original MyClass object.

But isn't "mc" in the method still pointing to the object? Kinda confused especially K&B 1.4, page 434 says "Since the method returns the ...object, it will not be eligible for collection even after the method has completed."
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable mc is local and therefore destroyed after the the method exits.

What they are saying is that, as you see in this case, when you return the value of mc, then a reference to the object is passed back to the caller of the method, so there is an additional reference to the object, and until you do something that removes all ways of accessing the object from a live thread, the object won't be eligible for garbage collection.
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Keith.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A reference has been returned and not the object itself ... after the method call there are two references pointing to the same object on the heap. But after the method invocation the local variable gets destroyed and hence only one reference exists 'x'. But since 'x' is made to point to a new object ..the old object has no reference pointing to it and hence is eligible for garbage collection.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of making a new topic to ask the exact same question, I'm bringing this old dead thread back to life b/c I'm still confused.


If I'm understanding things right (which I likely am not), this makes a new TestClass object, but will not automatically run the following method contained in TestClass:


So, if it doesn't run that method automatically (b/c the method will only be run when an object reference calls it via the dot operator), then why does the following cause the original object to become eligible for GC?



Doesn't this make x and mc point to the same object? I understand the local variable mc gets destroyed, but wouldn't the object itself still have a reference pointing to it (x)?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic