Easily learn JVM (4) & mdash; garbage collection algorithm, learn jvm garbage collection Algorithm

Source: Internet
Author: User

Easily learn JVM (4)-garbage collection algorithm and jvm garbage collection Algorithm

We all know that the biggest difference between the java language and the C language is the automatic memory reclaim, so how does the JVM control the memory reclaim? This article will introduce several JVM garbage collection algorithms, so as to understand the basic principle of memory recovery.

Stop the world

Before introducing the garbage collection algorithm, we need to first understand the word "stop the world". the stop the world will be generated when a garbage collection algorithm is executed, and the JVM will execute garbage collection, it will temporarily execute java applications and continue running after garbage collection is completed. If you have tested the java program using JMeter, you may find that there is an irregular pause in the java program during the test. In fact, this is actually "stop the world ", during the pause, the JVM is doing garbage collection. Therefore, minimizing the time to stop the world is the main objective of JVM optimization. Next, let's take a look at some common garbage collection algorithms.


As the name suggests, the reference counting method is to count the number of times an object is referenced. When a reference counting is added, 1 is added. If a reference counting is reduced, 1 is reduced.

It indicates that the reference of three Teacher objects points to the Teacher object in the heap. The reference count of the Teacher object is 3, and the reference count of the Student object is 2.

Indicates that the reference of the Teacher object is reduced to 2, and the reference of the Student object is reduced to 0 (because the reference points to null, for example, teacher3 = null), according to the reference counting algorithm, the memory space of the Student object will be reclaimed.

The reference counting algorithm is a very simple principle and is the original recycling algorithm. However, this algorithm is not used in java for two reasons. 1 is the impact of frequent counts on performance, 2 is the issue that it cannot handle circular references.

For example, the Student object is referenced in the Teacher object and the Teacher object is referenced in the Student object. In this case, the object will never be recycled.

Mark Clear

The tag clearing algorithm is the basis of many garbage collection algorithms. in simple words, there are two steps: tag and clear.

MARK: traverse all GC Roots and set the objects reachable from GC Roots to surviving objects;

Clear: traverses all objects in the heap and clears objects not marked as reachable;

Note that gray objects are not marked as surviving objects because they cannot be traversed from the GC Root (although they are referenced, they cannot be traversed from the GC Root, it will be recycled during cleanup.

Note that during the execution of the tag clearing algorithm, "stop the world" will be generated, so that the java program can pause and wait to ensure that the mark is cleared, no new objects are generated. Why must I suspend the java program? For example, if an object is generated after the tag process is complete and the object has missed the tag period, this newly generated object will be cleared as an inaccessible object because it is not marked, so the program will have an error. Therefore, when the markup clearing algorithm is executed, the java program will be suspended, generate "stop the world ".

Next we will summarize the mark clearing algorithm:

1. because it involves a large amount of memory traversal work, the execution performance is low, which may lead to a long period of "stop the world" and lower java program throughput;

2. We noticed that after the object is cleared, the cleared object leaves a memory vacancy location, resulting in memory discontinuous and space waste.

Next, let's see if other algorithms can improve these problems?

Mark Compression

You may have already thought of the tag compression algorithm. It adds the compression process based on the tag clearing algorithm.

After the mark is cleared, the memory space is compressed to save memory space. This solves the problem of memory discontinuity in the mark clearing algorithm.

Note that the mark compression algorithm also produces "stop the world" and cannot be executed concurrently with java programs. Some object memory addresses change during compression. java programs can only continue after compression is complete.

Copy Algorithm

In simple terms, the replication algorithm splits the memory into two parts, but only uses one of them. During garbage collection, it copies the surviving objects in the memory being used to another blank memory, finally, the objects in the memory space being used are cleared to complete garbage collection.

Compared with the label compression algorithm, the replication algorithm is more concise and efficient, but its disadvantages are obvious. It is not suitable for survival objects because many objects need to be copied, resulting in poor replication performance, therefore, the replication algorithm is often used for garbage collection in the new generation of memory space, because the new generation has fewer surviving objects and the replication cost is low. Another drawback of this mechanism is the high cost of memory space occupation, because it performs object replication based on two copies of the memory space, and only one copy of the memory space is used during the non-garbage collection period, resulting in a low memory utilization.


We have introduced common garbage collection algorithms. These algorithms have their own advantages and disadvantages, but they are not simply used in JVM, instead, we use something called a garbage collector. The garbage collector can be seen as a different combination of a series of algorithms. In different scenarios, we can use a suitable Garbage Collector to get twice the result with half the effort. Next we will introduce the garbage collector.



Actual Java Virtual Machine Ge yiming

Zhou Zhiming, a deep understanding of Java Virtual Machine (version 2nd)

Related Article

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.