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).
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.
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.
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.
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?
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".
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
Post by:autobot
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