Supplementary examples of java Memory leakage and java Leakage

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.