Forums Register Login

Memory leaks in Java

+Pie Number of slices to send: Send
Hi

Is it possible to have memory leaks in Java?
Is stackoverflow/outofmemory error an example of memory leak?
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

P.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Hi Petr,

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

m()
{
1.Object o1 = new String("one");
2.Object o2 = new String("two");
3.Object o3 = new String("three");
4.Vector v = new Vector();
5.v.add(o1);
6.v.add(o2);
7.v.add(o3);
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?
+Pie Number of slices to send: Send
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,
Petr
+Pie Number of slices to send: Send
I think that obvious example is static collection.
+Pie Number of slices to send: Send
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.
Bill
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1809 times.
Similar Threads
OutOfMemoryException
Memory leak problem
Performance issues
cannot see PIA Server from weblogic console
how to get rid of java.lang.outof memoryerror
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:13:17.