Java garbage Collection

Source: Internet
Author: User

1. Garbage collection

A Java garbage collection (GC) is divided into two phases:

Determine which objects can be recycled and use the accessibility analysis algorithm;

Recycling, recycling of those useless garbage objects, commonly used algorithms are: tag-clear algorithm, copy algorithm, marker-collation algorithm;

2. Accessibility analysis

The current mainstream virtual machines use the Accessibility analysis algorithm to determine whether an object is still alive.

The Java Virtual machine tags a series of objects called GC root, the basic idea of the accessibility analysis algorithm is to determine whether there is a reference path from these GC root objects to an object, and if so, the object is still useful, otherwise it is determined that the object is a garbage object, can be recycled.

Available as GC root objects are:

(1) objects referenced in the virtual machine stack (local variables in methods)

(2) object referenced by static property of class in method area

(3) method to go to the object in the constant reference

(4) objects referenced in the local method stack (local variables in the native method)

3. Tagging algorithms

The specific process that marks the death of the object (which is bound to be recycled):

(1) Determine whether an object can be reached (using the Accessibility analysis algorithm), if not reached, will be marked as recyclable;

(2) Then the first screening, to determine whether the object needs to execute the Finalize method, if the object does not overwrite the Finalize method or has already executed the Finalize method, you do not need to perform a Finalize method, otherwise you need to execute a Finalize method;

(3) If the Finalize method is not required, the object is directly determined to die, otherwise the object is added to a f-queue queue, waiting to execute the Finalize method;

(4) Then the second screening, the implementation of the F-queue queue of the object's Finalize method, if the object in the Finalize method is referenced again, then the object will not be recycled, otherwise, objects will be recycled;

4. Garbage collection algorithm

The virtual machine decides to reclaim an object that has already been marked as dead, and it needs to use the garbage collection algorithm. The commonly used garbage collection algorithms include: Tag-purge algorithm, copy algorithm, marker-collation algorithm, and the replication algorithm and marker-collation algorithm are the improvements to the mark-sweep algorithm.

(1) Mark-Sweep algorithm

The mark-and-sweep algorithm is the most basic and simple algorithm, which first marks the dead object with the tag algorithm described above, and then recycles the memory space of all tagged objects.

  

Where blue represents the surviving object, gray indicates the memory that has not been allocated, and yellow indicates the memory to be reclaimed.

This algorithm has a large drawback, and there is a lot of memory fragmentation in the memory after the tag is cleared.

(2) Copy algorithm

Using the replication algorithm, the available memory is divided into the same size of two parts A and b,a used to allocate memory to the object, b reserved. When a does not have enough space to allocate, it copies all the surviving objects to B and cleans up the memory space in a.

  

The advantage of the replication algorithm is that it is efficient to clean up the memory and does not generate a lot of memory fragmentation, but half of the memory is not utilized and memory waste is too serious.

(3) marker-Collation algorithm

Unlike the mark-and-sweep algorithm, when a dead object is tagged, the object is not recycled directly, but the surviving objects are moved together, compact memory, and then the remaining memory space is cleaned up.

  

(4) Generational recovery algorithm

In fact, the virtual machine will be divided into the new generation and the old age, the new generation of memory using an improved replication algorithm for memory recovery, the old age using the tag-collation algorithm for memory recovery.

  

  

In the Cenozoic, the memory is divided into three parts, one Eden section, two survivor parts, and the default Eden and survivor size ratio in the hotspot virtual machine is 8:1. Each time you use the Eden section and one of the survivor parts, you need to reclaim the two parts, use the copy algorithm, copy the surviving objects to another survivor, and then clean up the previous Eden and survivor parts.

Statistics show that objects in the Cenozoic memory are usually short-lived, so when memory is reclaimed, the surviving objects can usually be accommodated in another survivor (B). If another survivor (b) fails to accommodate all surviving objects, try to replicate to that survivor (b), and then copy the remaining objects that cannot be accommodated to the old age.

Java garbage Collection

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.