• 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

Leaks in a Garbage Collection System

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!
My doubt is from R&H page.22 in which he explains Leaks in Garbage collection System.
His description is as follows
I this example, assume the array "Stroage" is being used to maintain the storage of a stack. This pop() method is inappropriate:
1. public Object pop(){
2 return storage[index--];
3. }
If the caller of this pop() method abandons the popped value, it will not be eligivle for garbage collection until the array element containing a reference to it is overwritten. This might take a long time. You can speed up the process like this:
1. public Object pop(){
2 Object returnValue = storage[index];
3 storage[index--] = null;
4. return returnvalue;
5 }
The concept of what he is trying to tell is not getting to me. First I didn't get what he means
by saying "method abandons popped value" then the sentence "array element containing a reference
to it is overwritten"
Can somebody explain be elaborately of what is happening above.
Regards
Ajay.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ajay:
I believe what the authors mean by "the caller of the method abandons the popped value" is that the program which called the
method has no further use for the object reference which the
method returned. However, unless either:
(1) that object reference is replaced by a null reference as in the revised code sample; or:
(2) that object reference is replaced by a reference to another object (presumably via a "push" method or its equivalent)
the garbage collection system has no way of knowing that it is
safe to reuse the memory holding the object being pointed to by
the "popped" object reference.
Hence their statement "until the array element containing a
reference to it is overwritten".
Hope this helps.

 
Ajay Nalamala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the explanation Bob! I got my doubt cleared.
Regards
Ajay.
 
Ajay Nalamala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the explanation Bob! I got my doubt cleared.
Regards
Ajay.
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic