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

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question on "isolation a Reference" discussed in kathy and Bert' book on Garbage Collection

The code example:



My question is

After i4.i = i2; i3, i4 and i2 are formed as Island Objects so they are already eligible for garbage collection, is that right? So why they have to be nulled to be garbage collected?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The nature of automatic garbage collection has an important consequence:
You can still get memory leaks. If you allow live, accessible references to
unneeded objects to persist in your programs, then those objects cannot be
garbage collected. Therefore, it may be a good idea to explicitly assign null into a variable when you have finished with it. This issue is particularly noticeable if you are implementing a collection of some kind.
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. Here I am refering to the idea of islands of isolation. So if Island objects are formed, they are elibible for garbage collection.

1. I understand assigning null is a good practice. But is there a difference as to assigning null to them and not assigning null to them?

2. If asked after which line of code, i3, i4 and i2 will be eligible for garbage collection, can I say after the line "i4.i = i2;"?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is that as long as the objects are accessable from your main() method using i2, i3, or i4, the objects cannot be garbage collected. If you can reach one object, you can reach them all because they form a ring of references.

So you must set i2, i3, and i4 all to null before any of the three objects can be garbage collected.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'can I say after the line "i4.i = i2;"?'

No. If you can picture it, at this point you have a ring of three joined together kites each with a string attached to your hand. As Mike explained, you must let go of all those strings for the kites to be blown away. Even though they stay connected to each other.
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! I am clear.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic