Some experience of JVM

1. Start with small examples

环境是
java version “1.8.0_191”
Java™ SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot™ 64-Bit Server VM (build 25.191-b12, mixed mode)

openjdk version “1.8.0_191”
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.16.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

First, there is a simple garbage collection example,

class TestGC{
    public static void main(String[] args) {
        new TestGC();
        System.gc();//显式调用Full GC(),给出一个回收新老生代的信号(注意,不是一定会回收,因为jvm也会偷懒),一般是自动的,这里作为展示而手动触发
        System.runFinalization();   //强制调用对象的终结方法,这里才是真的销毁(事无绝对,隐式调用gc()时,有的对象如果在二次标记时找回组织,可以继续存活)
    }
}

View GC after compilation

The environment in the figure below is HotSpot jdk8 of windows

jdk8(Figure 1)
View detailed java heap usage (for non-standard java commands, please refer to oracle official explanation )
java -XX:+PrintGCDetails -XX:+UseG1GC
Explain the above command, which is to print the garbage collection details (plus the printing timestamp, when using the G1 garbage collector) But no effect)
figure 2(Picture 2)

The environment in the figure below is openjdk8 under Ubuntu
Insert picture description here(Figure 3)
Insert picture description here(Figure 4)
and the results are very interesting.

Types of Full GC G1 GC
jdk Small memory footprint and long time consuming Heap memory usage is small, time is unknown
openjdk Large memory footprint and short time-consuming Heap memory takes up too much, the time is unknown

What is Full GC?

Minor GC: Only the young generation YoungGen (including Eden, Survivor) is collected, but from Figure 2 and Figure 4, we find that young and survivors are separated, so this is interesting.
Major GC: Only clean up the old generation. However, this definition is not appropriate, because it will not only clean up the old generation, and is often triggered by minor GC. These two cannot be separated too much.
Full GC: Clean up all.

2. G1 involves terms

1、Metaspace

There is a big change here. Originally, there is a permanent generation PermGen in jdk7, but here jdk8 is completely removed and switched to Metaspace.

The official JDK8 HotSpot JVM explanation is a small goal achieved because of the integration with JRockit.

Hotspot JVM uses local memory to store class metadata information and calls this area Metaspace . Compared with the previous PermGen, meta-space expansion is easier and can accommodate more class data.

2. Mixed GC Event

This is a mixed GC event. Pay attention to the environment information written at the beginning, which contains build 25.191-b12, mixed mode , which indicates that the JVM defaults to mixed mode. During this time, all the young generation regions and a part of the old generation regions are recycled together.

3. Region

The G1 collector partitions the heap and divides it into regions. Each time it collects, select some regions to control the pause time of garbage collection. This region is the region, also known as the heap region. Usually divided into three areas Eden, Survivor, Old, and Survivor can also be divided into From and To areas.

4. Reclaimable

In order to be able to reclaim objects, G1 GC creates a series of heap regions dedicated to storing these reclaimed objects. These regions are all in a linked list queue, and the survival rate is defined by -XX:G1MixedGCLiveThresholdPercent.

5. RSet

The full name is Rememberd Set, which stores the mutual references of objects between different regions and participates in the reference reachability analysis when traversing the region root. The data structure used to record this in HotSpot is OopMap. This will make the GC of each partition more independent.

6. CSet

The full name is Collection Set, which saves the region that will be garbage collected in a GC. During GC, all live data in CSet will be transferred (copy/move). The heap area can be Eden, Survivor, and/or Old Generation, or none, that is, a blank area. The concept of region is smaller than that of Eden. In other words, Eden will have many regions . The size of the region is the same, it is a contiguous block of virtual memory, and CSet cannot account for more than 1% of the JVM memory.

7. G1 Pause Time Target

It is the G1 pause target time. Because GC is exclusive, it will cause the application to pause. This pause time is the set expected time. In G1, a pause prediction model is used to predict the target pause time set by the user, and garbage collection is performed based on this time.

8. PLAB

Promotion Local Allocation Buffers, used for young generation recycling. Its function is to avoid multi-thread competition for the same resource, each thread has an independent PLAB for Survivor and Old. Here is a digression, the object was first created in Eden as Allocation, and then because of the long existence, it slowly promoted to Survivor, and then Old. The two Allocation and Promotion have the same meaning in usage.

9. TLAB

Thread Local Allocation Buffers, the thread local allocation buffer, is a thread-specific memory allocation area. Avoid conflicts caused by multi-thread competition during object allocation. For G1, TLAB is a Region of Eden, and a single thread uses it to allocate resources. The main purpose is to allow a thread to exclusively use memory space through stack operations for object allocation, which is much faster than sharing between multiple threads.

Three, JVM memory model

This is very important, but many people write it very well, so I won’t go into details and quote it directly.
https://blog.csdn.net/u011552404/article/details/80306316
https://blog.csdn.net/wangshuminjava/article/details/80921305
Because the model is a framework, you can understand the overall operation of the jvm The mechanism and details still need to be considered.

Four, G1 GC

G1 is a brand new garbage collector officially appeared in jdk7. From a long-term vision, it is to replace the CMS collector. It still distinguishes between the young generation and the old generation, but the structure of the heap is adjusted so that the entire Eden area, the young generation or the region contained in the old generation is not required to be physically continuous. The new partitioning algorithm has the following characteristics:
parallelism (multiple GC threads work at the same time), concurrency (alternating execution with application threads), space defragmentation (different from the mark-cleaning of CMS and defragmentation after multiple GCs. G1 will clean up every GC), predictability (due to the partition, G1 only selects a part of the area for recycling, no longer a large pile of blocks)

Parallel loop of G1

Including the following activities: initial marking, parallel Root interval scanning, parallel marking, remarking and cleaning. Except for the final cleanup phase, all others belong to the phase of marking live objects.
1. In the initial marking phase, all GC roots are collected. Exclusive.
2. Parallel Root interval scanning must scan and mark all object references in the survivor interval, and the application can also be executed in parallel, but there is a time constraint, that is, it must end before the next GC starts.
3. Parallel marking phase, where basically all marking work is completed. Multithreading is used to mark live objects and corresponding logical maps in parallel, but application throughput will decrease.
4. The re-marking phase is an exclusive phase, usually a short pause, at which point all marking work is completed.
5. During the cleaning phase, regions that do not contain surviving objects will be recycled and added to the available region queue.

Guess you like

Origin blog.csdn.net/u014377853/article/details/88763948