A few days ago wrote a memory leak article. This article describes the memory leaks related knowledge: http://blog.csdn.net/u010590685/article/details/46973735
But the sample given here is not very good, see a very good example here today to add to everyone.
If we write a stack by ourselves. Here's how it's pop:
publicpop(){ Object object=arrays[size]; size--; return object; }
In this way we can see that the pop is returning the last one of the current array, and then moving the subscript forward, so that when we do not reference the object in the detailed business process, the Java Virtual machine still cannot reclaim the memory. Because the arrays value is the holder of its reference, this creates a memory leak. Solutions such as the following:
publicpop(){ Object object=arrays[size]; arrays[size]=null; size--; return object; }
We manually set the reference of the object that is out of the stack to null, so that there is no memory leak.
Java Memory leak Supplemental sample