aspose file tools*
The moose likes Beginning Java and the fly likes Garbage collection and memory leaks question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage collection and memory leaks question" Watch "Garbage collection and memory leaks question" New topic
Author

Garbage collection and memory leaks question

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107

Hello everyone,

Now that I'm finished with graduate school I'm going to concentrate on getting certified in Java and I'm studying with a book called "Complete Java 2 Certification" by Phillip Heller and Simon Roberts. Right now I am learning about memory leaks. I understand the concept alright and the adverse effects it can have, and that is one of Java's claims to fame (that there is automatic garbage collection) but there is a code snippet that has me befuddled and that is the following:





I have searched this site for hints and found the topic but I am still not clear on it. Specifically, how does storage[index--] = null prevent a memory leak, and what is the original value of index? I understand that if the value is null then it can be garbage collected but how does one know what index minus 1 is if there is no original value stated? Am I just to assume that it is documented somewhere else in the program and not in this snippet? Wouldn't index have to equal 0 for it to become null after index--1? Can I just assume that or am I missing something else?

If someone can elaborate a little on it for me I would appreciate it. It's probably a very basic thing to learn but my mind has turned mush from writing all those graduate papers instead making me smarter............

Well, thanks for any help!
Vonny
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Nice to see you back.

Can you find a copy of Bloch's Effective Java? There is a good description of code almost identical to that in it. If not, ask again.
Mazer Lao Tzu
Ranch Hand

Joined: Jan 20, 2010
Posts: 35
The index piece is simply managing a stack. In a stack, pop is one of the core methods (along with push). When reading this method, you can assume that at the start of the method, index is set to the index of the topmost element of the stack. That being said, the exact value of index is not necessary for demonstrating garbage collection.

Basically, the line

removes a reference to the object about to be returned.

A piece of memory taken by an object can be safely garbage collected only when all references to the object are eliminated. If this line was not here, then the reference to the object contained in the storage array would stick around indefinitely (or at least until that element of the array got overwritten).



-- Mazer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Lots of languages have automatic garbage collection, even the FORTH implementation I am struggling with. There is a good description of garbage collection in Object-Oriented Software Construction by Bertrand Meyer. All the .NET languages support it too. The problem is that C/C++ don't support memory management when used in operating systems, so you have to use free or delete whenever you create pointers. The tiniest error can lead to memory leaks because data remain in memory for ever.

If you haven't found Bloch's book, I can elaborate on Mazer's explanation.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(For C++ there are a number of smart pointer implementations, though. Which Forth are you working with now?)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
A reversible version developed by Frank Zeyda and Bill Stoddart about 3 years ago. The compiler I am trying to write should work on any standard FORTH, however, because I am not using any reversible features.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(Oh, cool; keep me posted--I'm a Forth oldbie and like it a *lot*.)
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Campbell, Mazer's explanation makes sense but I think I need a review of pop and push for it to make even more sense. Maybe there is something I can read on this site? I never heard about Bloch's book but I looked it up on Amazon and it got five stars average rating so maybe I will check it out. I already have Head First, which so far is the best one I've read, for an amateur like me, at least.

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Oh, I forgot to ask: what is Forth? And is anyone here certified? I'm thinking of taking one of those certification courses where they fly you to the class and pay for the hotel, and all that. Of course, it's expensive but it might be worth it for a week. I wonder if anyone has ever found those boot camps helpful.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Forth is a language.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Yes, I had gathered that already.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
FORTH is written in postfix. You can't write 1 + 2 but 1 2 +. It is a language developed in the 1960s and still in common use, but less popular than Java.

The code quoted is for a stack, based on an array. Let us imagine your push method looks like this and you already have 99 elements and you are pushing your 100th. Count was 99, but at the point where you add the element to the array, count is 100 and count++ is the old value (99), so your 100th element goes into the array in exactly the right place. Let's imagine all goes well and you have successfully pushed the 100th element.Now you have a count of 100, and you may wish to remove the 100th element with the pop method. If the pop method looks like this, and you take it off, then both count and --count are 99 in the end line of this method.When I said "remove", that wasn't accurate. You still have 100 elements in your array, but you have no way to gain access to your 100th element (at the index 99). What will happen if you pop lots more things off the stack and don't push anything else onto it? Answer, they will remain as real references which occupy memory, and you can't get rid of them. The garbage collector won't delete them, because they are still regarded as accessible, and you can't get at them because a stack is designed not to allow access to "pop"ped elements. Now you have a memory leak. It might not appear serious if you have 100 elements, but what if those elements each occupy 3MB? The only ways to get rid of them would be to push things on top of them, which hardly helps, or set the entire stack to null, which is probably not what you need to do.

If you try to get rid of the element from the array by setting it to null, your pop method will look like this, remembering that --count and count are still equal to 99 in the "Foo fff =" statement:You copy the reference from the array into a local variable, which needs very little memory, then you set the original value in the array to null. Now you have a returned value which you can use in your code, but when you have finished with it, there is no longer a reference to it in the array. So the garbage collector can easily delete it from memory, and all is well.

Have you done a search for Bloch's book? You should be able to find a sample chapter on the net, so you can look at the chapter and try before you buy.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
That's a nicely detailed explanation, Campbell. I always thought that Java automatically got rid of all useless objects and variable references but now I see why nullifying these values allows the program to clean them up and release the memory. There's so much to learn in Java, but that's why it is so interesting. Sometimes I think I'll never get very good at it because I have to much to learn yet but on the other hand that's what keeps me going.

Once again, thanks for the great help!

Vonique
Karthick Dharani Vidhya
Ranch Hand

Joined: Feb 23, 2008
Posts: 88
If of interest, please take a look at this thread regarding memory management


Karthick D V
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
That isn't Java, what I told you yesterday, but general programming. You can get exactly the same problem in any language. If you have a language with automatic memory management (which usually requires a virtual machine), it will "think" the reference is still needed.

If you have to do your own memory management, you can mistakenly think you have got rid of all references, and I don't know what will happen if you use the delete keyword (C++) or the free function (C); you will be deleting something from memory which you might still have a reference to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection and memory leaks question