GC of the JVM in the interview record

Immediately after the last one, it is mainly about HotSpot JVM

1. Introduce the GC algorithm

garbage collection

1.1 How to Identify Trash

1.1.1 Reference Counting

In Java, references and objects are associated. An object is used counter +1, and released timer -1. 当他的引用计数为 0 说明没人用了,就是可回收对象. But if there are two objects referencing each other, calculation errors will result.

1.1.2 Accessibility analysis

In order to solve circular references, Java uses reachability analysis, which uses a series of "GC root" objects as a starting point to search downwards. As long as the object cannot be found, it is an unreachable object. 不可达对象不是垃圾,经过 2 次不可达,才可以标记为可回收对象等待回收.

After determining the garbage, GC can be performed

1.2 Mark removal algorithm (Mork-Sweep)

The most basic garbage collection algorithm. Divided into 标记阶段and 回收阶段. Mark objects that can be recycled, and then recycle. But there is a disadvantage, 会使得内存碎片越来越多.
insert image description here

1.3 Copying algorithm (Copying)

An algorithm proposed to solve the defect of memory fragmentation in the mark-and-sweep algorithm. Divide the memory into two blocks of equal size according to the memory capacity. Only use one of them at a time. When this memory is full, copy the surviving object to another one to clear the used memory.
insert image description here
Although this algorithm is simple to implement, has high memory efficiency, and is not prone to fragmentation, the biggest problem is 可用内存被压缩到了原本的一半。且存活对象增多的话,Copying 算法的效率会大大降低.

1.4 Mark-Compact

Combining the above two algorithms, it is proposed in order to avoid defects. The marking phase is the same as the mark-clearing algorithm. Instead of cleaning up objects after marking, the surviving objects are moved to one end of the memory. Objects outside the end boundaries are then cleared.
insert image description here

1.5 Generational Collection Algorithm

The current mainstream VM garbage collection adopts the "Generational Collection" (Generational Collection) algorithm, which divides the memory into several blocks according to the different life cycles of objects, such as the new generation, old generation, and permanent generation in the JVM. The most appropriate GC algorithm can be used according to the characteristics of each age

1.5.1 New Generation and Replication Algorithm

At present, the GC of most JVMs adopts the Copying algorithm for the young generation, because each garbage collection in the new generation needs to
recover most of the objects, that is, there are fewer operations to be copied, but the new generation is not usually divided according to 1:1. Generally, the new generation is divided into a larger Eden space and two smaller Survivor spaces (From Space, To Space). Each time the Eden space and one of the Survivor spaces are used, when recycling, the two spaces are Copy the surviving objects in the Survivor space to another Survivor space, and clear the Survivor space as a whole.

1.5.2 Old Generation and Marking Algorithm

The old generation uses the Mark-Compact algorithm because only a small number of objects are recycled each time.

  1. The Permanet Generation in the method area mentioned by the JAVA virtual machine is used to store class classes, constants, method descriptions, etc. The collection of the permanent generation mainly includes obsolete constants and useless classes.
  2. The memory allocation of objects is mainly in the Eden Space of the new generation and the From Space of the Survivor Space (where Survivor currently stores objects), and in a few cases, it will be directly allocated to the old generation.
  3. When the Eden Space and From Space of the new generation are insufficient, a GC will occur. After the GC, the surviving objects in the Eden Space and From Space will be moved to the To Space, and then the Eden Space and From Space will be cleaned up.
  4. If To Space cannot store enough of an object, this object will be stored in the old generation.
  5. After GC, Eden Space and To Space are used, and the cycle repeats.
  6. When an object escapes a GC in the Survivor area, its age will be +1. By default, objects whose age reaches 15 will be moved to the old generation.

The maximum age of an object is 15 times, because the maximum of 4 bits in binary is 15. There are only 4 bits of age in the object header

1.6 Partition Algorithm

The partition algorithm divides the entire heap space into different consecutive small areas 每个小区间独立使用, 独立回收. The advantage of this
is that you can control how many small areas are recovered at one time, and according to the target pause time, several small areas (rather than the entire heap) can be recovered reasonably each time, thus 减少一次 GC 所产生的停顿.

2. Garbage collector

The relationship between 7 classic garbage collectors

1. A black line between two collectors indicates that they can 搭配使用.
2. The Serial Old is used as a backup plan for the "Concurrent Mode Failure" failure of the CMS.
3. (red dotted line) indicates removed. It will be in JDK8 Serial+CMS、ParNew+Serial Old这两个组合声明为废弃, and it will be completely canceled in JDK9.
4. (Green dotted line) In JDK14, the Parallel Scavenge+SerialOld combination is abandoned. 删除CMS垃圾回收器.

insert image description here

2.1 Serial garbage collector (single thread, copy algorithm)

Serial is the most basic garbage collector, using the copy algorithm, 曾经是JDK1.3.1 之前新生代唯一的垃圾收集器. Serial is an 单线程excellent collector, it will not only use one CPU or one thread to complete the garbage collection work, but also in the 进行垃圾收集的同时,必须暂停其他所有的工作线程,直到垃圾收集结束. Although the Serial garbage collector needs to suspend all other worker threads during the garbage collection process, it is simple and efficient, 对于限定单个 CPU 环境来说,没有线程交互的开销,可以获得最高的单线程垃圾收集效率so the Serial garbage collector is still the default new generation garbage collector for the Java virtual machine running in Client mode.

2.2 ParNew garbage collector (Serial+multithreading)

In fact, the ParNew garbage collector 是 Serial 收集器的多线程版本also uses the replication algorithm. In addition to using multi-threaded
garbage collection, the rest of the behavior is exactly the same as the Serial collector. The ParNew garbage collector also suspends all other worker threads during garbage collection. . By default, the ParNew collector opens the same number of threads as the number of CPUs, and the number of threads of the garbage collector can be limited by the -XX:ParallelGCThreads parameter.
Although ParNew is almost exactly the same as the Serial collector except for multithreading, it does ParNew 垃圾收集器是很多 java虚拟机运行在 Server 模式下【新生代】的默认垃圾收集器.

2.3 Parallel Scavenge collector (multi-threaded copy algorithm, efficient)

The Parallel Scavenge collector is also a garbage collector 新生代垃圾收集器that also uses the copy algorithm. 多线程It focuses on the program to achieve a controllable throughput (Thoughput, CPU time for running user code/CPU total consumption time, ie Throughput = running user code time / (running user code time + garbage collection time) ), high throughput can use the CPU time most efficiently and complete the calculation tasks of the program as soon as possible 主要适用于在后台运算而不需要太多交互的任务. The adaptive tuning strategy is also an important difference between the ParallelScavenge collector and the ParNew collector.

2.4 Serial Old Collector (Single-threaded Marking Algorithm)

Serial Old is the Serial garbage collector 年老代版本, which is also a 单线程special collector, using the mark-sorting algorithm. This collector is also the default old generation garbage collector that runs on the default java virtual machine of the Client. In Server mode, there are two main purposes:

  1. Used in conjunction with the new generation of Parallel Scavenge collectors in versions prior to JDK1.5.
  2. As a fallback garbage collection scheme using the CMS collector in the old generation.

2.5 Parallel Old Collector (Multithreaded Marking Algorithm)

The Parallel Old collector 是Parallel Scavenge的年老代版本, which uses a multi-threaded mark-sort algorithm, was only available in JDK1.6.
Before JDK1.6, the ParallelScavenge collector used in the new generation can only be used with the Serial Old collector in the old generation. It can only guarantee the throughput of the new generation first, but cannot guarantee the overall throughput. Parallel Old is just for the old generation. Also provides a throughput-first garbage collector, 如果系统对吞吐量要求比较高,可以优先考虑新生代 Parallel Scavenge和年老代 Parallel Old 收集器的搭配策略.

2.6 CMS Collector (Multithreaded Mark Sweep Algorithm)

The Concurrent mark sweep (CMS) collector is a year 老代垃圾收集器whose main goal is 获取最短垃圾 回收停顿时间to use a multi-threaded mark-sweep algorithm, unlike other old generations that use mark-sort algorithms.
Minimal garbage collection pauses can improve user experience for highly interactive programs.

The working mechanism of CMS is more complicated than that of other garbage collectors. The whole process is divided into the following four stages:

  • The initial mark
    is just to mark the objects that GC Roots can be directly associated with, which is very fast and still needs to suspend all worker threads.
  • Concurrent marking
    The process of GC Roots tracking works with user threads without suspending worker threads.
  • Remarking
    In order to correct the marking records of the part of the object whose marking changes due to the continuation of the user program during concurrent marking, it is still necessary to suspend all worker threads.
  • concurrent purge

2.7 G1 Collector

Garbage first garbage collector is the most cutting-edge achievement in the development of garbage collector theory. Compared with the CMS collector, the two most prominent improvements of the G1 collector are:

  • Based on the mark-sort algorithm, 不产生内存碎片.
  • The pause time can be controlled very precisely, in 不牺牲吞吐量前提下,实现低停顿垃圾回收.

The G1 collector avoids full-area garbage collection. It divides the heap memory into several independent areas of fixed size, and tracks the progress of garbage collection in these areas. At the same time, it maintains a priority list in the background. Each time, according to the allowed collection time, The areas with the most garbage are collected first. The area division and priority area recycling mechanism ensures that the G1 collector can obtain the highest garbage collection efficiency in a limited time.

Guess you like

Origin blog.csdn.net/qq_37761711/article/details/130354837