File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Creating leaks to Garbage Collection?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Creating leaks to Garbage Collection??" Watch "Creating leaks to Garbage Collection??" New topic

Creating leaks to Garbage Collection??

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Apr 08, 2002
Posts: 51
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

[Blog] [Blogroll] [My Reviews] My Linked In
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
yeah thanx guys.....n Val I guess now my name's fine ...
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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
I agree. Here's the link:
subject: Creating leaks to Garbage Collection??
jQuery in Action, 3rd edition