Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Memory leaks in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory leaks in Java" Watch "Memory leaks in Java" New topic

Memory leaks in Java

Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87

Is it possible to have memory leaks in Java?
Is stackoverflow/outofmemory error an example of memory leak?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

It is possible to have memory leaks in Java, which are usually the result of bad coding. An OutOfMemeoryException is a possible result of this (though getting one doesn't guarentee a memory leak).

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87
Thanks Paul,

Can you give me a simple example of a bad code, that could result in memory leak?
As far as I undestand, memory leak is possible in C, where you allocate memory using malloc and then do not close the pointer.
Or when an unreferenced memory byte is accessed, say trying to access the
10+i th byte of an array whose size is 10.
How is this possible in java? especially when there is no way the programmer has a way to directly access a memory location.
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
Hi Vasim,

Quite a stupid example would be:

Now image that function doSomething throws an exception.
The the object it was processing won't get removed on the next
line and won't get garbage collected even if oX = null;
was called, because it is still it the bigSet!

In this example it is a bit too obvious, but this is the principle
of that. It is usually very well hidden.


Get a better web browser:<br /><a href="" target="_blank" rel="nofollow"></a>
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Vasim Patel:

Is stackoverflow/outofmemory error an example of memory leak?

Stack overflow is something else. It means that there was not enough room to store the call tree and local variables. This is usually as a result of very high level of recursion.

The default stack size is pretty big and I have never run out of stack, other than because of uncontrolled recursion, due to a programming error. However, the Sun JVM, at least, has stack size parameters that can be changed. I think it's -Xss, but that's just from memory - you should consult documentation.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87
Hi Petr,

Thanks for the example. Please let me know if my understanding is correct. In the example below.

1.Object o1 = new String("one");
2.Object o2 = new String("two");
3.Object o3 = new String("three");
4.Vector v = new Vector();
8.o1 = null;
9.o2 = null;
10.o3 = null;
11. v = null;


after line 1, reference count of the String object created at line 1 is "1".
after line 5, the reference count of the above string object becomes "2".
so line 8 will not result in the String object created at line 1 to be garbage collected.

1.Which means none of the objects created at lines 1,2,3 will be GCed after line 10. Am I correct here?

2. What happens at after 11? I am making v to null, which will result in Vector created at line4 to be GCed but have not "removed" any objects from the vector. Do this mean that after line 11, String objects created at 1,2,3 will be GCed?
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
Hi Vasim,

Lines 8,9,10,11 can be removed without any impact. (And the same
applies to the o[123] = null; in my example.) All of your variables
are used locally in the function m only. When they get out of scope
they will become eligible for GC. The difference between your and
mine example is that there is a "global" variable (bigSet) in mine
and I expect it exists "as long as the program runs".

Forget about reference counting. It is an implementation detail and
you don't care how they do it. To cut it short, all will get eligible.
I am afraid I can't explain it just using words. There is a short
explanation in the book "Hardcore Java".

Best regards,
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
I think that obvious example is static collection.

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12835
Lets make the distinction between "memory leak" caused by bad program design and "memory leak" caused by underlying problems in the language and core libraries.
The only memory leak caused by core libraries that I have heard of was(still is?) in the javac compiler which only showed up in servlet containers compiling many JSP.
There are PLENTY of bad program design memory problems.
I agree. Here's the link:
subject: Memory leaks in Java