Java Memory leak Supplemental sample

Source: Internet
Author: User
Tags object object

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

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.