Introduction to Common GC Algorithms

tags: # java virtual machine  jvm  GC

This article covers a brief introduction to several commonly used garbage collection algorithms, including:

  • Mark-Sweep Algorithm (Mark-Sweep)
  • Copying algorithm (Copying)
  • Mark Compression Algorithm (Mark-Compact)
  • Generational Algorithm (GenerationalCollecting)
  • Partition algorithm (Region)

The content and screenshots are all from "Practical Java Virtual Machine: JVM Troubleshooting and Performance Optimization (2nd Edition)"

Mark-Sweep Algorithm (Mark-Sweep)

The mark-and-sweep method is the ideological basis of modern garbage collection algorithms. The mark-and-sweep method divides garbage collection into two phases: the mark phase and the sweep phase.

In the marking phase, all reachable objects starting from the root node are first marked by the root node. Therefore, unmarked objects are unreferenced garbage objects. Then, in the cleanup phase, all unmarked objects are cleaned up. The biggest problem with the mark-and-sweep method is the potential for space debris.

Copying algorithm (Copying)

The core idea of ​​the copy algorithm is: Divide the original memory space into two blocks, and only use one of them each time. During garbage collection, copy the surviving objects in the memory being used to the unused memory block, and then Clear all objects in the memory block being used, swap the roles of the two memories, and complete garbage collection.

If there are many garbage objects in the system, the number of surviving objects that the replication algorithm needs to replicate will be relatively small. Therefore, at the moment when garbage collection is really needed, the efficiency of the replication algorithm is very high.

And because the objects are uniformly copied to the new memory space during the garbage collection process, it can be ensured that the recovered memory space is not fragmented. Although it has the above two advantages, the cost of the copy algorithm is to halve the system memory. Therefore, the simple copy algorithm is also difficult to accept.

Improved Copy Algorithm

In Java's new generation SerialGC collector, the idea of ​​copy algorithm is used. The new generation is divided into three parts: eden area, from area and to area. Among them, the from area and the to area can be regarded as two spaces with the same size, equal status and interchangeable roles for replication. The from area and to area are also called the survivor area, that is, the survivor space, which is used to store objects that have not been recycled.

During garbage collection, the surviving objects in the eden area will be copied to the unused survivor area (assumed to be the to area), and the young objects in the in-use survivor area (assumed to be from) will also be copied to the to area (large objects Or the old object will directly enter the old age, if the to area is full, the object will also directly enter the old age). At this time, the remaining objects in the eden area and the from area are garbage objects, which can be emptied directly, and the to area stores the surviving objects after this recycling. This improved copy algorithm not only ensures the continuity of space, but also avoids a lot of waste of memory space, as shown in the figure below.

Mark Compression Algorithm (Mark-Compact)

Mark compaction is an old generation collection algorithm. It has made some optimizations based on the mark-and-sweep method. Like the mark removal method, the mark compression method first needs to start from the root node and mark all reachable objects once. But then, instead of simply cleaning up unmarked objects, it compacts all surviving objects into one end of memory. Then, clean up all space outside the bounds. This method not only avoids fragmentation, but also does not require two identical memory spaces, and is cost-effective.

Generational Algorithm (GenerationalCollecting)

Due to the different characteristics of the new generation and the old generation, different algorithms are used for different generations.

The new generation generally uses the replication algorithm, for example, the following garbage collectors are based on the replication algorithm:

  • SerialGC
  • ParNewGC
  • ParallelGC

The old generation generally uses mark-sweep or mark-compact algorithms, such as the following garbage collectors:

  • CMS
  • ParallelOldGC

partition algorithm

The partitioning algorithm divides the entire heap space into different consecutive small areas, as shown in the following figure. Each cell is used independently and recycled independently. The advantage of this algorithm is that it can control the number of reclaimed cells at one time.

Generally speaking, under the same conditions, the larger the heap space, the longer the time required for a GC, and the longer the resulting pause. In order to better control the pause time generated by GC, a large memory area is divided into multiple small blocks. According to the target pause time, several small areas are reclaimed reasonably each time instead of reclaiming the entire heap space, thereby reducing one GC resulting pause.

Introduction to G1 Collector

The G1 collector (GarbageFirst) is a new garbage collector officially used in JDK1.7. From a long-term perspective, it is to replace the CMS collector. The G1 collector has a unique garbage collection strategy, which is completely different from the previously mentioned collectors.

From the point of view of generation, G1 is still a generational garbage collector. It will distinguish the young generation from the old generation, and still have the eden area and the survivor area. Or the old generation is continuous.

It uses partitioning algorithm. As a long-term alternative to CMS, G1 uses a brand-new partition algorithm with the following features:

  • Parallelism: During G1 recycling, multiple GC threads can work simultaneously to effectively utilize multi-core computing capabilities.
  • Concurrency: G1 has the ability to execute alternately with the application, and part of the work can be executed at the same time as the application. Generally speaking, the application will not be completely blocked during the entire recycling period. ·
  • Generational GC: G1 is still a generational collector, but unlike previous collectors, it takes into account both the young generation and the old generation. Other collectors either work in the young generation or in the old generation. ·
  • Space defragmentation: G1 will carry out appropriate object movement during the recycling process. Unlike CMS, it simply marks and cleans up objects. After several GCs, CMS must perform a defragmentation. Unlike G1, it effectively copies objects every time it recycles, reducing fragmentation space. ·
  • Predictability: Due to partitioning, G1 can only select some areas for memory recovery, which narrows the scope of recovery, and the global pause can also be better controlled.

Intelligent Recommendation

Introduction to common algorithms in Python

First, the algorithm introduction 1. What is the algorithm? The algorithm refers to the accurate and complete description of the solution, and is a series of clear instructions to solve the problem. T...

Introduction of common hashing algorithms

background knowledge: Hash algorithms are mainly used in cache distributed systems. Distributed means that a whole is divided into multiple parts, which are executed by multiple servers. Pay attention...

Introduction to common encryption algorithms

Introduction As a developer, it is necessary to understand some encryption algorithms. Through encryption algorithms, we can accomplish three goals of data communication, namely data confidentiality, ...

Introduction to common sorting algorithms

Insertion sort(Insertion sorting): Insert a piece of data into ordered data to form new ordered data with a length plus one. It is suitable for sorting a small amount of data. The time complexity is O...

Common GC algorithms in Java_garbage collector and memory allocation_G1 garbage collector

Common GC algorithms Reference counting: Every object has a counter, the object is referenced once, the counter is +1, when the object reference fails once. The counter -1, when the object counter is ...

More Recommendation

How does the JVM determine that an object is recyclable? What are the common GC algorithms?

1. How to judge whether an object is dead in java? Death means it can be recycled 1. Reference counting algorithm: Add a reference counter to each object. When a reference points to an object, the cou...

Garbage collection mechanism and common GC algorithms in Java virtual machines

1. Common GC algorithms 1. Mark-clearing algorithm Mark the objects that need to be recycled and then clean them directly in GC. Disadvantages: It will cause a large number of discontinuous memory fra...

Introduction to machine learning and common algorithms

concept What is machine learning? Machine learning is a literal translation of the English name Machine Learning (ML). Machine learning involves many disciplines such as probability theory, statistics...

Introduction to common algorithms for data encryption

data encryption Foreword 1.MD5 encryption 2.RSA encryption 3. AES encryption 4. 3DES encryption Foreword Whether user data is stored in SharePreferences or in a SQLite database, it is necessary to enc...

Introduction to common interpolation algorithms (3)

2019 Unicorn Enterprise Heavy Gold Recruitment Python Engineer Standard >>> Bicubic interpolation (also called coordination plate element), the binary bicubic interpolation formula has a tota...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top