[Effective Java Reading notes] Chapter II Creating and destroying objects sixth-? Reviews

Source: Internet
Author: User

Sixth elimination of expired references

There will still be memory leak in Java, such as a stack growth and contraction, then the object pops from the stack will not be garbage collected, the immediate use of the stack of the program no longer reference these objects. This is because the inside of the stack maintains outdated references to these objects (which are never dismissed) and needs to be shown to empty (null) the objects and tell the garbage collector to reclaim them.

For example, in the implementation of the Stack class, the pop () function will eventually have null operations on the element:

     Public synchronizede pop () {e obj; intLen =size (); Obj=Peek (); Removeelementat (Len-1); returnobj; }     Public synchronized voidRemoveelementat (intindex) {... .. ........... elementdata[elementcount]=NULL;/*To-let GC do it work*/    }

In addition to the memory leaks on the stack, there is a memory leak on the cache, that is, putting the object in the cache and forgetting it, as well as listeners and other callbacks, this looks less serious ....

[Effective Java Reading notes] Chapter II Creating and destroying objects sixth-? Reviews

Related Article

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.