Introduction to the

Java programmers have all heard of GC, and everyone knows that the purpose of GC is to scan the heap space and then release objects marked for deletion from the heap space to increase the available heap space. Today we’re going to take a look at the secret behind GC, Safepoints.

Garbage collector for GC

Brother F, why are there so many kinds of garbage collectors in GC? It’s not too much trouble to use. And I heard CMS has been scrapped in JDK9zhong.

So many garbage collectors were actually built during the development of the JVM. In previous articles, we talked about how there are several types of GARBAGE collectors out there.

  1. Reclaimer based on generation technology

Concurrent Mark Sweep (CMS), an updated version of Mark and Swap, uses multiple threads to scan the heap area for efficiency.

CMS has been deprecated in JDK9 due to parameter complexity and performance issues.

Serial garbage collection, which uses a single thread for garbage collection, has the advantage of not having to interact with other threads. If you have a single-core CPU, it’s best to choose Serial Garbage collection because you can’t take full advantage of multi-core. It is also often used on smaller projects.

Parallel garbage Collection, if you are a multi-core processor, Parallel GC might be your choice.

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

G1 garbage collection,G1= garbage First, was created to replace CMS and First appeared in java7.

G1 divides the heap area into smaller areas, each of which is labeled as either Young generation or old generation. Thus running the GC in a smaller area rather than affecting the entire heap area.

  1. A collector that is not based on generational technology

Z Garbage Collection, ZGC is an extensible, low-latency GC. ZGC is concurrent and does not require stopping the running thread.

ZGC was introduced in JDK11.

There are also other GCS under development.

More highlights:

  • Blockchain from getting started to Giving up series tutorials – with ongoing updates covering cryptography, Hyperledger, Ethereum,Libra, Bitcoin and more
  • Spring Boot 2.X Series tutorials: Learn Spring Boot from Scratch in seven days – continuous updates
  • Spring 5.X Series tutorials: Everything you can think of in Spring5 – constantly updated
  • Java Programmer from Handyman to Expert to God (2020 edition) – Ongoing updates with detailed articles and tutorials

Problems in generational recyclers

Brother F, isn’t the generation recycler good? Why are there new recyclers based on non-generational technologies such as the ZGC?

A very common phenomenon in generational garbage collectors is “Stop The World”. What is Stop the world?

This means that during GC, all threads need to suspend execution in order to collect garbage. All threads are suspended.

Of course, The G1 is based on generational technology, but it doesn’t actually “Stop The World.”

The JVM defines a number of Root objects, and starting with these objects, identifies the objects they reference to form an object graph. All objects in the graph are valid objects; otherwise, objects not in the graph should be reclaimed. Valid objects will be marked as alive.

These Root objects include: local objects in the executing method and input parameters. An active thread that loads static fields and JNI references in the class.

safepoints

For STW to work, the JVM needs to provide a mechanism for all threads to stop at the same time. These stops are called safepoints.

Note that these stopped threads do not include threads running Native code. These threads are not managed by the JVM.

Code execution in the JVM can be done either by JIT compilation into machine code or by interpretation.

In JIT, the inspection code is compiled directly into machine code. By setting the corresponding marker bit, so as to execute the suspension instruction in the process of thread running.

Again, take the JMH example we mentioned in the previous article:

@Benchmark
    public void test1(a) {
        int length = array.length;
        for (int i = 0; i < length; i=i+1)
            array[i] ++;
    }
Copy the code

Let’s look at its assembly code:

You can see that there is a test directive, which is the generated Safe points.

By setting the flag bit, you can suspend the thread while it is running.

In the case of interpreted execution, the JVM keeps two bytecode scheduling tables, and when safePoint is needed, the JVM switches the table to enable SafePoint.

What is SafePoint used for

In general, GC, JIT anti-code optimization, code cache flushing, class redefinition, partial lock cancellation, and other debug operations.

We can use – XX: + PrintGCApplicationStoppedTime to print safepints pause time.

+ PrintSafepointStatistics – – XX: XX: PrintSafepointStatisticsCount = 1 these two parameters can force the JVM print safepoint some statistics.

conclusion

Safepoint is a very important concept in garbage collection that I want you to understand.

Author: Flydean program stuff

Link to this article: www.flydean.com/jvm-jit-saf…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!