Little teacher learning JVM: GC's garbage collection algorithm

Posted Jun 16, 20206 min read

Introduction

The importance of the JVM is self-evident. If you compare the application of java to a sports car, then the JVM is the engine of this car. Without it, the java program becomes a castle in the sky, without root duckweed. In the JVM, there is a memory area called the runtime data area, which stores all the objects needed at runtime, and the Heap Area is the largest one of them.

After all, memory is not unlimited, so we need a mechanism to recycle objects that are no longer used. This mechanism is the GC we will talk about today.

More exciting content and see:

Object life cycle

Little Sister:Brother F, do you believe that this world has reincarnation?

Brother I am a staunch atheist, just live in the moment, why bother with the reincarnation?

Little sister:Brother F, you don t understand this. Consciousness is a combined pattern of the atomic groups that make up the brain. The physical basis of our brain is no different from a stone. When we master the way the brain is assembled and then reconstruct, our consciousness reappears. This is reincarnation. This is the concept mentioned in quantum theory.

Wow, when did the younger sisters become so powerful, they began to discuss such advanced topics. Brother F, I really can't keep up.

Little sister, Brother F, I am afraid that you will be embarrassed and want to bring up the topic of the life cycle of java objects.

I'm not familiar with quantum theory, and I haven't been afraid of java objects.

The life cycle of an object is actually very simple:it is created, used, and finally destroyed.

  1. Create an object

For the simplest example of creating an object:

Object obj = new Object();

When an object is created, a specific space will be allocated for the object.

  1. Target

After the object is created, it can be used by other objects. If other objects use the object, then we become the object to be referenced.

  1. Object destruction

When an object is not referenced by other objects, we call the object can be recycled. In Java, GC is responsible for the collection of objects.

Garbage collection algorithm

Little teacher:Brother F, I think garbage collection seems to be quite simple. We maintain a pointer counter for each object and add one every time it is referenced. Isn't it possible to implement a garbage collector?

The underlying principle is such a truth, but the JVM needs a more efficient algorithm to ensure the efficiency of garbage collection, and it will not affect the running program.

Next, we will introduce several commonly used garbage collection algorithms in the JVM:

Mark and sweep

Mark and sweep is the simplest garbage collection algorithm. Simply put, it can be divided into two steps:

  1. Mark the live object

Marking live objects sounds simple, just scan the objects in the heap to see if they are introduced.

But there is a problem here. If two objects refer to each other, and these two objects are not actually referenced by external objects, then these two objects should actually be recycled. So we also need to solve a key problem:where to start scanning.

The JVM defines some Root objects. Starting from these objects, find the objects they refer to to form an object graph. All objects in this graph are valid objects, otherwise objects that are not in the graph should be recycled. Valid objects will be marked as alive.

These Root objects include:local objects and input parameters in the method being executed. Active thread, loading static fields and JNI references in the class.

Note that this kind of traversal actually has a disadvantage, because in order to find out which objects are live in the object graph, the entire application must be suspended to make the objects static, so that an effective object graph can be constructed. Later we will introduce a more effective garbage collection algorithm.

  1. Delete an object

After scanning the objects, we can delete the unmarked objects.

There are three ways to delete, the first way is to delete normally. However, normal deletion will result in memory fragmentation. So the second way is to compress after deletion to reduce memory fragmentation. There is also a way to delete the copy, which means copying the alive object to a new memory area, which can also solve the problem of memory fragmentation.

Concurrent mark sweep(CMS)

Before talking about CMS, let s talk about Eden, Old, and Survivor space in the garbage collector, which are generational technologies that everyone should be familiar with.

Young Gen is divided into 1 Eden Space and 2 Suvivor Space. When the object was just created, it was placed in Eden space. During garbage collection, Eden Space and a Suvivor Space will be scanned. If the objects in Eden Space are still valid during garbage collection, they will be copied to another Suvivor Space.

In this way of continuous scanning, in the end, after multiple scans, the still valid objects will be put into Old Gen to indicate that their life cycle is relatively long, which can reduce garbage collection time.

The next few garbage collectors, except ZGC, all use generational technology.

Okay, now I will continue to talk about CMS. CMS is an upgraded version of mark and swap. It uses multiple threads to scan the heap area to improve efficiency.

CMS uses mark-copy in Young Generation, and mainly uses mark-sweep in Old Generation.

The command to use CMS is simple:

-XX:+UseConcMarkSweepGC

The above are some tuning parameters of CMS listed.

Serial garbage collection

Serial garbage collection uses a single thread for garbage collection operations, the advantage is that it does not need to interact with other threads. If you are a single-core CPU, then it is best to choose Serial garbage collection, because you can not make full use of the benefits of multi-core. Similarly, it is often used in relatively small projects.

Serial garbage collection uses mark-copy in Young Generation, and mainly uses mark-sweep-compact in Old Generation.

The following is the open command:

-XX:+UseSerialGC

Parallel garbage collection

Similar to serial GC, it uses mark-copy in Young Generation, and mainly uses mark-sweep-compact in Old Generation. The difference is that it is parallel.

You can specify concurrent threads with the following command:

-XX:ParallelGCThreads=N

If you are a multi-core processor, then Parallel GC may be your choice.

Parallel GC is the default GC in JDK8. After JDK9, G1 is the default GC.

Use the following command to start Parallel GC:

-XX:+UseParallelGC

G1 garbage collection

Why is it called G1, G1=Garbage First, it was born to replace CMS, and it first appeared in java7.

G1 divides the heap area into multiple smaller areas, each of which is marked as young or old generation. Thus running GC runs in a smaller range, rather than affecting the entire heap area.

You can use the following command to open:

-XX:+UseG1GC

Z Garbage Collection

ZGC is a scalable, low-latency GC. ZGC is concurrent, and there is no need to stop running threads.

Use the following command to start:

 -XX:+UseZGC

ZGC was introduced in JDK11.

How to choose

Little sister:Brother F, you talked about so many GCs, which one should I use?

Anti-aircraft guns can't be used to fight mosquitoes, so choosing the right GC is the ultimate thing. Here, Brother F gives you a few suggestions:

  1. If your application memory is already small, then use the serial collector:-XX:+UseSerialGC.
  2. If your program runs on a single-core CPU and there is no limit to the program pause time, then use the serial collector:-XX:+UseSerialGC.
  3. If the performance requirement for the peak period is relatively high, but there is no requirement for the program pause time, then you can use the parallel collector:-XX:+UseParallelGC.
  4. If you pay more attention to the response time, and the pause time of the GC must be small, you can use -XX:+UseG1GC.
  5. If response time is very important, and you are using a large amount of heap space, you can consider using ZGC:-XX:UseZGC.

to sum up

This article introduces several GC algorithms, you can choose according to your needs.

Author:flydean program those things

Link to this article: http://www.flydean.com/jvm-gc-algorithms/

Source of this article:flydean's blog

Welcome to pay attention to my public number:those things of the program, more exciting waiting for you!