2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Author: ofLJli
Link: https://juejin.cn/post/7003213289425633287?searchId=20240709085629749958B21D886D4E67D4
Source: Rare Earth Digging
Copyright belongs to the author. For commercial reproduction, please contact the author for authorization. For non-commercial reproduction, please indicate the source.
The main structures in JVM are: virtual machine stack, heap, and method area. The size of the stack frame of the virtual machine stack is determined by the compiler. With the end of the method or the technology of the thread, the memory of the virtual machine stack is also recycled. However, the Java heap and method area have significant uncertainty. The allocation and recycling of this part of memory are dynamic. What GC really cares about is how to manage this part of memory.
This article focuses on the following three things that GC needs to accomplish:
According to two generational hypotheses:绝大多数的对象都是朝生夕死
,熬过越多次的GC回收的对象就越难回收
The heap is divided into generations: new generation (Eden, From, To) and old generation, and generational recycling is also performed during GC.
Minor GC: Recycle unused objects in the new generation. Most of the new generation objects are born and die overnight. The triggering time is:
标记-复制算法
Reclaim this area (discussed later).Major GC: Reclaim unused objects in the old generation. Generally used标记-清除算法
or标记-整理算法
Recycling.
Full GC: Recycles unused objects in the heap and method area. Full GC has a large recycling range and a long execution time, which may cause lags, so try to reduce the number of Full GCs. The triggering time is roughly:
The old generation has insufficient space, and new generation objects move into the old generation, large objects move directly into the old generation, etc. If these objects cannot be stored in the largest continuous space of the old generation, a Full GC recovery will be performed.
Insufficient space in the method area The method area mainly stores type information and constant pool. There is also a risk of insufficient space, and Full GC will be performed.
System.gc() is explicitly called and Full GC is performed
Principle: Use可达性分析算法
Unavailable objectsmark, and then performClearDisadvantages: When there are many objects, the marking efficiency is low. After clearing the objects, memory fragmentation and discontinuity will occur. Function: Some collectors use this algorithm in the old generation collection.
Principle: Divide the memory space into two, half for storing objects and half for free. If the area for storing objects is full, use可达性分析算法
Move the surviving objectsmarkCome out, thencopyGo to another empty area, and at the same time clear all the previous areas to become empty continuous space. Disadvantages: If there are many surviving objects, a large amount of memory copying and opening up is required. Only half of the memory space can be used, which wastes resources. Function: This recycling algorithm is generally used for objects that are born and die in the new generation. However, the copy algorithm is optimized in the new generation, but this algorithm adds an allocation guarantee mechanism to prevent the situation where too many surviving objects cannot be allocated. An Appel-style recycling algorithm is used:
Principle: The marking process is标记-清除算法
Same, then整理存
The live objects move to one end, and then all the objects outside the survival boundary are清除
Disadvantages: There are certain risks in moving objects. Too many objects are not efficient. Effect: Mainly affects the old generation.
GC uses garbage collectors for recycling. With continuous development, there are more and more garbage collectors. Here are the conventional garbage collectors and divide them into three categories: single-threaded collector, multi-threaded collector, and concurrent collector.
The combination of single-threaded collectors is: Serial/Serial Old collectors. They not only use one collection thread to complete the collection operation, but also when the collection thread is working, the user thread must stop and wait until the collection is completed. The figure is a schematic diagram of the Serial/Serial Old collector:
If the client's memory resources are limited, the number of processor cores is small or for a single-core processor, its simplicity and efficiency can enable the collector to complete its work as quickly as possible.
The multi-threaded collectors are: ParNew, Parallel Scavenge, Parallel Old, among which Parallel Scavenge/Parallel Old is a combined collector. These multi-threaded collectors only add garbage collection threads, and user threads still stop waiting for garbage collection.
parNew collector: It is actually a multi-threaded version of Serial, and can currently cooperate with the Serial collector and CMS collector.
Parallel Scavenge Collector: Generally used with Parallel Old Collector. Compared with parNew Collector, it pays more attention to吞吐量
The throughput isUser thread execution time as a percentage of total CPU execution time, of course, the higher the throughput, the better.
Multithreading is generally used on the server side, because the execution of multithreading consumes time in the rotation of time slices, which will undoubtedly slow down the processing efficiency for a single processor. However, for servers with good resources and no need to interact with users, the execution efficiency can be increased.
The concurrent collectors are: CMS collector, which aims to minimize system downtime and provide a better user experience. Its collection thread can be executed concurrently with the user thread. CMS has three标记
(initial marking, concurrent marking, remarking) and once清理
(Concurrent cleanup), two of the three markings require a shorter user thread stop, a longer marking concurrent with the user thread, and a cleanup concurrent with the user thread.
Initial Mark: Mark the first object associated with GC Roots, the time is very short Concurrent marking: Execute the reference chain of GC Roots (reachability analysis algorithm) concurrently with user threads, which takes a long time Re-label: Re-search the new reference chain generated by the user thread during the concurrent marking phase. This takes a little longer than the initial marking. Concurrent cleanup:use标记-清除算法
Clear out useless objects.
Three major disadvantages: 1. CPU sensitivity. Concurrency may affect the operation of user threads on processors with fewer cores. 2. Floating garbage: Garbage generated during the concurrent cleanup phase can only wait for the next GC to recycle. 3. Memory fragmentation. Mark-sweep will produce a large amount of discontinuous memory space.
This article from那些内存需要回收
,什么时候回收
,如何回收
As an implementation, two object survival judgment algorithms, the conditions for Class area recycling, the generational mechanism and collection timing of recycling, three collection algorithms and commonly used garbage collectors were written out.