• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating leaks to Garbage Collection??

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For SCJP what topics do
we have to cover regarding Garbage Collection .....also I cudnt understand this code given in RHE under the topic Causing leaks to a Garbage Collection System ....can ayone please explain...
the Code goes:

public Object pop()
{
Object returnValue = storage[index];
storage[index--] = null ;
return returnValue;
}
� � � � �
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't do that(set the storage[index--] to null), the values you returned before are still in the storage,right?
that will cause the leak of the memory.
[ April 09, 2002: Message edited by: andy lau ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SMalik,
Welcome to Javaranch
I'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
[ April 12, 2002: Message edited by: Valentin Crettaz ]
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I wouldn't really call this a memory leak. To me, its more of a memory optimization. If you think about it, if it is an array of objects, then it has a predetermined size. So the array is already set to the total it can possibly hold. It won't be leaking memory, it will just be holding memory that it doesnt need, but you've already set the array to a desired max size, so you'll never get over your predetermined size.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry Joshua, I'm with Andy.
Note that the array is not local. Thus when the returned object looses its references it is still holded by the array and it can not be garbage collected.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah thanx guys.....n Val I guess now my name's fine ...
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that Jose.. but I just wouldnt call it a memory leak.. Let me try to explain what I was trying to say ..Lets say we have an array that we know will at most hold 20 objects.. So we set it to 20 objects. Now, we know that sometimes it may hold 20 objects.. so we plan for it. We will never go over our planned 20 object size, even if we don't set the objects to null. So, to me, its not really a memory leak, its just not optimized. I think of a memory leak as causing us to use more memory than we planned to use. In this case, we arent, because we planned for 20 objects, if we use them or not is not the issue. I might even run faster without the necessary null calls
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic