Supplementary examples of java Memory leakage and java Leakage
A few days ago I wrote an article on Memory leakage, which introduced the knowledge of Memory leakage: http://blog.csdn.net/u010590685/article/details/46973735
However, the examples provided here are not very good. Today we can see a good example to add to you.
If we write a stack by ourselves, the pop method is as follows:
Public Object pop () {Object object = arrays [size]; size --; return object ;}
In this method, we can see that pop returns the last bit of the current array, and then moves the subscript forward by one, in this case, even if we do not reference this object in a specific business process, the Java Virtual Machine still cannot recycle this memory, because the arrays value still holds its reference, this results in Memory leakage. The solution is as follows:
Public Object pop () {Object object = arrays [size]; arrays [size] = null; size --; return object ;}
We manually set the reference of the object in the stack to null; this will not cause memory leakage.
Copyright Disclaimer: This article is an original article by the blogger. For more information, see the source.