JVM garbage collection algorithm and jvm garbage collection

Source: Internet
Author: User

JVM garbage collection algorithm and jvm garbage collection

1. Reference Counting Collector)

The reference counting algorithm is a simple but inefficient algorithm. Its main principle is as follows: each object in the heap has a reference counter; when the object is referenced, add 1 to the reference counter; when the referenced object is left empty or out of scope, the reference counter minus 1. This method cannot solve the problem of mutual reference, so JVM does not adopt this algorithm.

2. Tracing Collector)

Tracking and recycling algorithms use JVM to maintain object reference graphs, traverse object application graphs from the root node, and mark the objects to be traversed. After the traversal is complete, the unmarked objects are currently not used and can be recycled.

3. Compacting Collector)

The main idea of the compression and recycling algorithm is as follows: Move the objects in the heap to one end of the heap, so that a large idle area will be set aside at the other end of the heap, it is equivalent to processing fragments in the heap. Although this method can greatly remove heap fragments, each processing will result in performance loss.

4. Copy the recycle algorithm (Coping Collector)

The main idea of the replication and recycling algorithm is as follows: the heap is divided into two regions of the same size. At any time, only one of these regions is used until this region is exhausted, at this time, the recycler will interrupt the execution of the program. By traversing it, all the active objects will be copied to another region. During the copying process, they will be arranged next to each other, this eliminates memory fragments. After the replication process is completed, the program runs until this area is used up, and then the above method is used to continue garbage collection.

The advantage of this algorithm is that objects are arranged while garbage collection is performed, thus eliminating memory fragments. But it also paid a very high price: for a specified size of heap, it requires two times the size of memory space. At the same time, the execution of the current program should be interrupted during the adjustment process, this reduces the point-to-point efficiency of the program.

5. Generational Collector)

  The by-generation collection algorithm JVM divides the heap into the Young, Old, and Perm areas. The Young area is further divided into the Eden area and two vor areas. All newly created objects are in the Eden area, when the Eden area is full, minorGC will be triggered to copy the objects that are still alive in the Eden area to one of the two vor areas, and the objects in the other region vor area will also be copied to the same region vor area, to ensure that there is always an empty region vor. The Old zone stores objects that are still alive after minorGC is triggered when the busy vor in the Young zone is full, when the Eden area is full, objects will be stored in the same vor area. If the same vor area still cannot store these objects, the GC collector will store these objects directly in the Old area. If the objects in the same vor area are Old enough, they are directly stored in the Old area. If the Old area is Full, Full GC is triggered to recycle the entire memory. The Perm area stores Class objects. If a Class is frequently loaded, the Perm zone may also be Full, and garbage collection in the Perm zone is also triggered by Full GC.

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.