GC Algorithm of JVM

Source: Internet
Author: User
Tags garbage collection

1. Preface

1.1 Concept: Clean up objects in memory that are no longer in use

1.2 Background: If the in-memory garbage is not cleaned, it can cause memory overflow

1.3 Commonly used garbage collection algorithms: Reference Counting Method (Reference counting), mark Clear Method (mark-sweep), copy Algorithm (Copying), tag compression method (mark-compact), generational algorithm (generational collecting) and partitioning algorithm (region)

2. Algorithmic Evolution

2.1 Reference Counting Method "Java garbage collection not adopted"

2.1.1 Thought: For object A, if referenced, A's reference counter is + 1; When the reference fails, A's reference counter-1. When object A's reference counter has a value of 0, object A is recycled.

2.1.2 Defects:

2.1.2.1 cannot handle the case of a circular reference. For example, object A and object B reference each other, but are not referenced by other objects. At this point, a and B should be recycled. However, the reference counters for objects A and B are not 0 and cannot be recycled. 】

2.1.2.2 each time the object is referenced/released, it will be accompanied by operation, which will have a certain effect on the system performance.

Because the objects that are referenced by the loop cannot be recycled, in order to solve the flaw, the concept of the object can be reached. Based on the concept of the accessible object, a mark removal method is produced.

2.2 Mark Removal Method "The basis of modern garbage collection algorithm "

2.2.1 Basics:

2.2.1.1 Object : A reference search through the root object that can eventually be reached.

2.2.1.2 Object : A reference search through the root object, and ultimately no object to be referenced to.

2.2.2 Thought: Garbage collection by marking and clearing two phases

2.2.2.1 Marking phase: Marks all the objects that can be reached according to the root node

2.2.2.2 Purge phase: Clears all unreachable objects

2.2.3 defects:

2.2.3.1 produces a lot of space debris, low productivity

The mark Clear method is to reclaim a contiguous space, which is a discontinuous "". In the process of object's heap space allocation, especially the memory allocation of large objects, the work of discontinuous memory space is less efficient than continuous space.

Because the non-contiguous memory space is inefficient, in order to solve this problem, then think: A new workspace, the object can be copied to the new memory space, to ensure the continuity of space use.

  

2.3 Replication Algorithm "Cenozoic algorithm"

2.3.1 thought: The original memory space is divided into two pieces, each using one piece. During garbage collection, the objects in memory that are in use are copied into unused memory blocks, clearing all objects in the memory block being used, swapping two memory roles, and completing the reclamation.

2.3.2 Defects:

2.3.2.1 the system to half the Passbook, the system memory occupied is too large.

The efficiency of the replication algorithm is based on the premise that there are few surviving objects and many objects of garbage objects. For many surviving objects, replication costs are high and other algorithms are needed to replace them.

2.4 Mark compression method " old age algorithm "

2.4.1 principle: After the tag cleanup algorithm executes, a memory defragmentation is done.

2.4.2 thought: Starting from the root node, mark all the objects that can be reached, and then compress the tagged object to one end of the memory. Finally, clear all the space outside the boundary.

In the above algorithms, they all have their own unique advantages, and no one algorithm can completely replace other algorithms. Therefore, the correct way is to use the appropriate algorithm to recycle according to the nature of the garbage collection object. Based on this situation, the idea of generational generation has been created.

2.5 Generation algorithm

2.5.1 thought: According to the characteristics of the object, the memory area is divided into several pieces, according to the characteristics of each area of memory, the use of different recovery algorithm.

2.5.2 Application: The new generation uses the replication algorithm, the old age uses the tag compression method.

Since the object's life cycle (temporal angle) can be recycled, it can be recycled depending on the object's spatial distribution (spatial angle).

  

2.6 Partitioning algorithm

2.6.1 background: Under the same conditions, the larger the heap space, the longer the time spent in a GC, and the longer the pauses are generated.

2.6.2 thought: Divide the heap space into contiguous small areas, each small area is used independently and recycled independently.

3. Supplement

3.1 Generation serial garbage collector, using the idea of a copy algorithm.

3.1.1 Foundation: The Cenozoic is divided into Eden area, from zone, to area. The From and to zones are also known as survivor Zones, which can be considered as two spatial blocks of equal size, equal status, and role-swapping for replication.

3.1.2 Idea: When garbage is collected, the objects in the Eden area and the survivor area (assuming the from zone) are copied to the unused survivor area (to Zone), then the Eden and from areas are emptied.

3.1.3 Note: Large objects or older objects in the From area will enter the old age area directly, and if there is insufficient space in the to area, the object will enter the old age area.

3.2 The efficiency of the replication algorithm is built on the premise that the object is few and cannot reach the object, so the copy algorithm is only used in the Cenozoic generation, the garbage object is usually more than the surviving object; Most of the objects in the old age are surviving objects. The old age uses the method of marking compression

3.3 Generation GC vs. old-age GC:

4. Appendix

4.1 References: "Practical Java Virtual machine JVM does fault diagnosis and performance optimization" Ge Yi

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.