A few days ago wrote a memory leak of the article, which describes the memory leaks related knowledge: http://blog.csdn.net/u010590685/article/details/46973735
But the example given here is not very good, see a good example here today to add to everyone.
If we write a stack ourselves, here is the Pop method:
publicpop(){ Object object=arrays[size]; size--; return object; }
In this method we can see that the pop is returning the last one of the current array, and then moving the subscript forward, so that the Java virtual machine still cannot reclaim the memory if the object is not referenced in our specific business process. Because the arrays value is the holder of its reference, this creates a memory leak, the workaround is as follows:
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.
Copyright NOTICE: This article is the original blog article, reproduced please indicate the source
Java Memory Leak Supplement Example