Chapter 4 JVM garbage collection algorithm and Chapter 4 jvm garbage collection

Source: Internet
Author: User

Chapter 4 JVM garbage collection algorithm and Chapter 4 jvm garbage collection

Note: Before reading this article, you need to know how to determine whether the object is alive or not. For details, see Chapter 3 JVM memory recycle area + object survival judgment + reference type + garbage collection thread.

Note: This article mainly refers to the distributed Java application: basic and practice, which is different from some of the statements in the in-depth understanding of Java Virtual Machine (version 2), but the principle is consistent.

 

1. Three garbage collection algorithms

  • Mark-clear (old generation)
  • Mark-organize (I .e. mark-compress) (elder generation)
  • Replication (young generation)

1.1 mark-clearing Algorithm

Principle:

  • Scan from the root collection node, mark all the surviving objects, and finally scan the entire memory space and clear the unlabeled objects (that is, dead objects)

Applicable scenarios:

  • It is more efficient when there are many surviving objects.
  • Applicable to the old generation (that is, the old generation)

Disadvantages:

  • It is easy to generate memory fragments, and then a relatively large object (typical case: the size of this object is greater than the size of each part in the idle table but smaller than the sum of the two parts ), garbage collection is triggered in advance.
  • Scan the entire space twice (First: Mark the surviving object; Second: Clear the unlabeled object)

Note:

  • In this case, the memory is irregular, and the object's memory allocation adopts the "Idle list method". For details, see Chapter 2.JVM Memory Allocation

 

1.2 mark Sorting Algorithm

Principle:

  • Scan from the root collection node, mark all the surviving objects, and finally scan the entire memory space and clear the unlabeled objects (that is, dead objects) (we can find that the front side is the principle of the Mark-clearing algorithm.) After clearing, move all the surviving objects left together.

Applicable scenarios:

  • Used for the old generation (that is, the old generation)

Disadvantages:

  • The object needs to be moved. if the object is very large and the memory after the mark is recycled is very incomplete, it may take some time to move this action.
  • Scan the entire space twice (First: Mark the surviving object; Second: Clear the unlabeled object)

Advantages:

  • No memory fragments

Note:

  • In this case, the memory is normalized, and the object's memory allocation adopts the "pointer collision method". For details, see Chapter 2 JVM memory allocation.

 

1.3 replication algorithm

Principle:

  • Scan from the root collection node, mark all the surviving objects, and copy these surviving objects to a new memory (the memory below the figure, then, all the original memory (the memory above the figure) will be reclaimed.

Applicable scenarios:

  • It is more efficient when there are few surviving objects
  • Scan the entire space once (mark the surviving object and copy and move it)
  • Applicable to the young generation (I .e. the New Generation): basically 98% of the objects are "born and died overnight", and few will survive.

Disadvantages:

  • An empty memory space is required.
  • Move objects to be copied

Note:

  • In this case, the memory is normalized, and the object's memory allocation adopts the "pointer collision method". For details, see Chapter 2 JVM memory allocation.
  • Change Time by space: reduces the number of scans by using an empty memory.

 

2. Garbage Collection Mechanism

According to Chapter 1 JVM memory structure, the young generation can be divided into Eden and survivor (two parts: from and to), and Eden: from: to =

1) The newly generated objects are preferentially distributed in the Eden area (unless-XX: PretenureSizeThreshold is configured, objects greater than this value will directly enter the old generation );

2) When the Eden area is full or cannot be placed, the surviving objects in the Eden area will be copied to the from area (Note that if the surviving objects in the from area cannot be stored, then all the surviving objects enter the old generation), and all the memory in the Eden area is recycled;

3) The generated objects will continue to be allocated to the Eden area. When the Eden area is full or cannot be placed, at this time, the surviving objects in the Eden and from areas will be copied to the to area (Similarly, if the surviving objects cannot be stored in the to area, then, all the surviving objects are in the old generation, and all the memory in the Eden and from areas is recycled;

4) in this way, many objects will be copied many times (each time the object is copied, the age of the object is + 1). By default, when the object is copied for 15 times (This number can be configured through-XX: MaxTenuringThreshold), it will enter the old generation.

5) when the old generation is Full or the old generation is not enough to enter the surviving objects of the old generation, a Full GC will occur (this is the most important thing we need to reduce because it takes a lot of time)

 

Summary:

  • Young Generation: replication algorithm
  • Old Generation: Mark-clear or mark-sort (the former is faster than the latter but generates memory fragments, compared with the former, the latter will not generate memory fragments, but it will be slower because it wants to move the surviving object)
  • The preceding method of using different collection algorithms for the young generation and the old generation is called the generation division collection algorithm, which is also a method used by enterprises.
  • Each algorithm is implemented by many different garbage collectors. In actual use, you can make a choice based on your own business characteristics.

 

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.