What is a Java garbage collector
The Java garbage collector is one of the three important modules of the Java virtual machine (JVM) (the other two are interpreters and multi-threaded mechanisms). It provides applications with automatic memory allocation (Memory Allocation) and automatic memory collection (Garbage Collect) functions. Both operations occur on the Java heap (a piece of memory is fast). At a certain point in time, if an object has more than one reference pointing to it, then the object is Live. Otherwise, it will be considered garbage and can be recycled and reused by the garbage collector. Garbage collection operations require CPU, threads, time and other resources, so it is easy to understand that garbage collection operations do not occur in real time (the object is released immediately after death). When the memory is consumed or a certain indicator is reached (Threshold, the ratio of memory to total memory is used, such as 0.75), the garbage collection operation is triggered. There is an exception to the death of an object. Even if an object of type java.lang.Thread is not referenced, as long as the thread is still running, it will not be recycled.
Recycling mechanism
According to statistical analysis, most objects in Java (including some other high-level languages) have short life cycles, so Java memory is managed in generations. The purpose of generational division is nothing more than to use different management strategies (algorithms) for memory blocks of different generations to maximize performance. Compared with the older generation, the younger generation is usually much smaller, and the recycling frequency is high and the speed is fast. The elderly have low recycling frequency and take a long time. The memory is distributed in the young generation. The objects in the young generation will be automatically promoted to the old generation after multiple recycling cycles.
Design Choices
Design selection affects the implementation difficulty of JVM garbage collector and JVM performance indicators, and is suitable for different scenarios. It describes the style characteristics of the recycling algorithm.
Single-threaded serial recycling vs multi-threaded parallel recycling
The question of whether the recycling operation itself is processed by multiple threads. The advantages of single-thread recycling are that it is simple, easy to implement, with few fragments, and is suitable for single-core machines. Multi-threaded parallel recycling can fully utilize CPU resources on multi-core machines, reduce recycling time and increase productivity. The disadvantage is that it is complex and some fragments may not be recycled.
Pause application thread during recycling vs. Recycling and application concurrent
Issue whether to pause the application thread during the recycling operation. The advantages of pausing application threads are that they are simple, accurate, cleaned up relatively clean, and cleaned up for a short time (exclusive of CPU resources). The disadvantage is that pausing application threads will cause the application response time to be extended during the garbage collection cycle, and systems with very high real-time performance are more sensitive. The advantages of parallel processing of recycling and application threads are that the application reaction time is relatively stable, and the disadvantages are that it is difficult to achieve, high cleaning frequency, and possible fragmentation.
Unmerge freed memory fragments VS Merge freed memory fragments VS Copy the alive to a new place
These three options describe how to manage dead memory block fragments. Dead memory fragments are usually scattered everywhere in the heap. If they are not managed, there will be two problems. When memory allocation is slow due to finding available memory, small fragments will lead to memory waste (such as large arrays require large continuous memory fragments). There are two ways to manage: move the living memory to one end of the memory block, record the starting position of available memory, or simply copy the living memory to a new memory area, and the original memory block is completely empty.
Performance Metrics
①. Productivity (throughput)
The ratio of non-recovery time to total time within a longer period (only long periods are meaningful). Measuring the operating efficiency of the system.
②. Garbage Collection overhead
The ratio of recovery time to total time over a longer period. Corresponding to productivity, the sum totals 100%.
③. Pause time
When Java virtual machines recycle garbage, some algorithms will pause the execution of all application threads, and some systems may be sensitive to the pause time interval.
④. Frequency of collection
How long does it take to recover?
⑤. The size of memory usage (Footprint)
Like the size of the heap.
⑥. Real-time (Promptness)
How long does it take for an object to occupy memory from the object to be recycled?
Types of garbage collection
All recycler types are based on generational technology. The Java HotSpot virtual machine includes three generations, the Young Generation, the Old Generation, and the Permanent Generation.
①Permanent generation
Stores classes, methods and their description information. The initial size and maximum value can be specified by the two optional options -XX:PermSize=64m and -XX:MaxPermSize=128m. Usually we don't need to adjust this parameter, the default permanent generation size is enough, but if there are too many classes loaded and not enough, just adjust the maximum value.
② Older generation
The main storage objects in the young generation that still survive and upgrade after multiple recycling cycles are still alive. Of course, for some large memory allocations, they may also be directly allocated to the permanent generation (an extreme example is that the young generation cannot save at all).
③Young generation
Most memory allocation and recycling actions occur in younger generations. As shown in the figure below, the young generation is divided into three areas, the original area (Eden) and two small survival areas (Survivors). The two survival areas are divided into From and To according to their functions. Most objects are allocated in the original area, and more than one garbage collection operation still survives are placed in the living area.
Serial Collector
A single thread performs a recycling operation, suspends the execution of all application threads during the recycling period. The default recycler in client mode is forced to be specified through the -XX:+UseSerialGC command line option.
①Minor Collection for young generations
Move the surviving objects in the Eden area to the To area. If the To area cannot be installed, move directly to the old generation. If the From area cannot be installed, move directly to the old generation. If the From area is very old, upgrade to the old generation. After the recycling is completed, both Eden and From areas are empty. At this time, the functions of From and To are exchanged, From are changed to To and To, and To is empty before each round of recycling. The design selection is replication.
②The recycling algorithm for the elderly (Full Collection)
The recycling of the elderly is divided into three steps: Mark, Sweep, and Merge. The marking stage marks out all surviving objects, the clearing stage releases all dead objects, and the merging stage merges all surviving objects into the previous part of the old generation, leaving all the free fragments behind. The design selection is merge to reduce memory fragmentation.
Parallel Collector
Use multiple threads to perform garbage collection at the same time. The multi-core environment can fully utilize CPU resources, reduce recycling time, increase JVM productivity, and the default recycler in Server mode. Like the serial recycler, execution of all application threads is suspended during recycling. Forced to specify via the -XX:+UseParallelGC command line option.
①Minor Collection for young generations
Multiple threads are used to recycle garbage, and the algorithm of each thread is the same as that of a serial recycler.
②The recycling algorithm for the elderly (Full Collection)
The older generation is still single-threaded, the same as the serial recycler.
Parallel Compacting Collection
The recycling of the younger generation and the older generation is processed by multi-threading. Specified by the command option -XX:+UseParallelOldGC, XX:ParallelGCThreads=3 can further specify the number of threads participating in parallel recycling. Like the serial recycler, execution of all application threads is suspended during recycling. Compared with parallel recyclers, the older generation has a shorter recycling time, thus reducing the pause time. Forced to specify via XX:+UseParallelOldGC command line option.
①Minor Collection for young generations
Same as Parallel Collector
②The recycling algorithm for the elderly (Full Collection)
The elderly are divided into three steps: marking, counting, and merging. The idea of division is used here, and the old generation is divided into many regions of fixed size. In the marking stage, all surviving objects are divided into N groups (the number of recycled threads should be the same as the number of recycled threads). Each thread is independently responsible for its own group, marking the location of the surviving objects and the survival rate information of the region (Region), and marking it as parallel. In the statistical stage, the survival rate of each region is counted. In principle, the survival rate in the front is relatively high. From front to back, the starting position worth merging is found (the areas where most objects survive are not worth merging). The statistical stage is serial (single thread). In the merge stage, based on the information in the statistical stage, multiple threads copy the surviving objects from one region to another region in parallel.
Concurrent Mark-Sweep Collector
Also known as the Low-latency Collector, the application is suspended for a shorter time through various means. Basically performs recycling operations concurrently with the application, without merging and copying operations. Specified through the command line -XX:+UseConcMarkSweepGC, you can also specify the use of incremental recycling mode -XX:+UseConcMarkSweepGC in single-core or dual-core systems. Incremental recycling refers to dividing the recycling operation into multiple fragments, executing one fragment and releasing CPU resources to the application, and then continuing to recycle the last result at a certain point in the future. The purpose is also to reduce delay.
①Minor Collection for young generations
Same as Parallel Collector
②The recycling algorithm for the elderly (Full Collection)
It is divided into four steps: Initial Mark, Concurrent Mark, Remark, and Concurrent Sweep. Pay special attention, there is no merge operation, so there will be fragments.
Performance evaluation tool for Java garbage collector
①XX:+PrintGCDetails and XX:+PrintGCTimeStamps
Information on the start time, duration, spare memory of each generation, etc.
②jmap [options] pid
jamp 2043 View shared objects that have been loaded in the 2043 process. Usually DLL files.
jmap -heap 2043 Check the configuration information and usage of the memory heap.
jmap -permstat 2043 Check the loading status of the permanent generation.
jmap -histo 2043 Check the loading and memory usage of the class.
③jstat [options] pid
jstat -class 2043 class loading, unloading, memory usage.
jstat -gc 2043 GC execution status.
postscript
Java provides automatic selection and automatic performance optimization functions. Before doing garbage collector tuning, first list the performance indicators you are concerned about, tell the JVM the performance indicators you are concerned about through the command line, and automatically tuned by the JVM. If you are not satisfied, you can specify the garbage collector. OutOfMemory is usually due to insufficient heap memory, so you can adjust the -Xmx1024m and -XX:MaxPermSize=128m command lines.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.