Java Recycle mechanism

Source: Internet
Author: User

Java memory recovery algorithm, mainly used in two ways, one is the tag counter way, one is the root directory search method.

Tag counter mode, is for each object if referenced, then mark the Counter plus 1, dereference, the marker counter minus 1. This object is reclaimed when an Object tag counter is 0 o'clock. However, there is a problem with this approach, and if there are cross-references to two objects, then the object value is null and cannot be reclaimed.

The root search method is used to search from a root directory object, which can be reclaimed if an object is unreachable.

When Java is recycled, there are two algorithms used, one is mark-erase and one is the copy algorithm.

Tag-purge algorithm: First mark the object that needs to be cleared in memory, then release the object once. However, the problem with this approach is that it is possible that the memory that is released is not contiguous and will cause insufficient memory if the request requires large contiguous memory.

Copy algorithm: The memory is divided into two parts, one for use and one for the other. When there is not enough memory, the portion of memory that is used is marked and then the surviving object is copied to the other part. After that, the memory used is emptied. (but this algorithm can cause a lot of wasted memory, and memory usage is reduced to half the original)

The common way is to use the combination of the above two types. Each use divides the memory into a larger Eden and two survivor areas, each using Eden and one of the survivor. When you need to clear, copy the data from Eden and one of the survivor tags to another survivor, and then clean up the data in Eden and survivor.

Reference strength: Weak reference, soft reference, strong reference, and virtual reference

The Java memory area is mainly divided into three districts. Young generations, old generations, lasting generations.

The young generation is divided into 3 districts, Eden,survivor0,survivor1. After countless times of minor GC, the surviving object is entered into the old generation. In older generations, the memory cleanup is full GC. The persistent generation is mainly for storing some static files, class information, classloader information and so on. Garbage collection has no significant impact on durable generations.

The Java garbage collector has cms,jdk1.8 a new G1 garbage collector.

Java Recycle mechanism

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.