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

Garbage Collection .....

Muralidhar Gandepalli

Joined: Apr 13, 2001
Posts: 13
Hi ,All
I have this question troubling me for some time ..."Inspite of having Garbage Collector in a VM can we have memory leaks ?.If yes can any one give one or two examples of these leaks "
Thanx in advance
Stefan Zoerner
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Yes, we can have Memory Leaks as in C or C++.
Condider the following program:

It causes an OutOfMemoryError in seconds. Of cause, this is not a real world example. But it shows, that just those objects are garbage collected, that are not in use. Not in use means, that there is no reference to it, i.e. it is not possibe to access the object any longer.
The many byte-arrays in my example still have references to them -- one each (the vector "knows" them). So the garbage collector can't decide to throw them away.
Programming errors can cause a similar effect. Of cause, that would not be that abvious. But forgetting to release references (e.g. by setting them to null) definetly can cause that situation. And at least one of these errors caused a memory leak in one of my projects ...
I hope that helps.
Greetings from Hamburg,

Stefan Zoerner
Author of German LDAP-Book
Committer at Apache Directory Project
subject: Garbage Collection .....
It's not a secret anymore!