What is the Java garbage collector
The Java garbage collector is one of the three important modules of the Java Virtual Machine (JVM) (the other two are the interpreter and the multi-threading mechanism). The application provides automatic memory allocation (Memory Allocation) and automatic recycling (Garbage Collect) functions. Both operations occur on the Java heap (a section of memory is fast). At a certain point in time, if an object has more than one reference (Rreference) pointing to it, then the object is alive (Live), otherwise it is dead (Dead) and is regarded as garbage and can be recycled and reused by the garbage collector. Garbage collection operations consume resources such as CPU, threads, and time. Therefore, it is easy to understand that garbage collection operations do not occur in real time (objects are released immediately after death). When the memory is consumed or reaches a certain indicator (Threshold, the used memory accounts for the total memory ratio, such as 0.75), the garbage collection operation is triggered. There is an exception for object death. Even if the object of type java.lang.Thread has no reference, it will not be recycled as long as the thread is still running.
Recycling mechanism
According to statistical analysis, most objects in Java (including some other high-level languages) have a short life cycle, so Java memory is managed in generations. The purpose of generational generation is nothing more than to use different management strategies (algorithms) for memory blocks of different generations to maximize performance. Compared with the old generation, the young generation is usually much smaller, and the recycling frequency is high and the speed is fast. The old generation has a low recycling frequency and takes a long time. Memory is allocated in the young generation. Objects in the young generation that still survive after multiple recycling cycles will be automatically promoted to the old generation.
Design Choices
Design choices affect the difficulty of implementing the JVM garbage collector and the performance indicators of the JVM. Applicable for different scenarios. Describes the style characteristics of the recycling algorithm.
Single-threaded serial recycling VS multi-threaded parallel recycling
The issue of whether the recycling operation itself is multi-threaded. The advantages of single-threaded recycling are that it is simple, easy to implement, has less fragmentation, and is suitable for single-core machines. Multi-threaded parallel recycling can make full use of CPU resources on multi-core machines, reduce recycling time, and increase productivity. The disadvantage is that it is complicated and some fragments may not be recycled.
Pause the application thread during recycling VS Recycling and application concurrently
The issue of whether to pause the application thread when recycling operations are performed concurrently. The advantages of pausing the application thread are simplicity, accuracy, relatively clean cleaning, and short cleaning time (CPU resources are exclusive). The disadvantage is that pausing the application thread will cause the application response time to be prolonged during the garbage collection cycle, and the real-time performance is very high. The system is relatively sensitive. The advantage of parallel processing of recycling and application threads is that the application response time is relatively stable, but the disadvantages are that it is difficult to implement, the cleaning frequency is high, and there may be fragments.
Do not merge the released memory segments VS merge the released memory segments VS copy the living ones to a new place
These three options describe how to manage dead memory blocks fragment. Dead memory fragments are usually scattered all over the heap. If not managed, there will be two problems. Memory allocation will be slow due to searching for available memory. Small fragments will cause a waste of memory (such as large array requirements). large contiguous memory segment). There are two ways to manage, move the live memory to one end of the memory block and record the starting position of the available memory, or simply copy the live memory to a new memory area, leaving the original memory block empty.
Performance Metrics
①、Productivity(Throughput)
Within a longer period (a long period is meaningful) , the ratio of non-recycled time to total time. Measures the operating efficiency of the system.
②. Garbage Collection overhead (Garbage Collection overhead)
The ratio of recycling time to the total time in a long period. Corresponds to productivity, which adds up to 100%.
③. Pause time interval (Pause time)
When the Java virtual machine is recycling garbage, some algorithms will suspend the execution of all application threads. In some systems, Might be sensitive to the pause interval.
④. Frequency of collection
How long does it take on average for recycling operations to occur.
⑤. Size of memory occupied (Footprint)
Such as the size of the heap.
⑥. Real-time (Promptness)
After the death of an object, how long does it take for the memory occupied by the object to be recycled.
Types of garbage collection
All collector types are based on generational technology. The Java HotSpot virtual machine contains three generations: Young Generation, Old Generation, and Permanent Generation.
①Permanent generation
Storage classes, methods and their description information. You can specify the initial size and maximum value through the two optional options -XX:PermSize=64m and -XX:MaxPermSize=128m. Usually we do not need to adjust this parameter. The default permanent generation size is sufficient. However, if there are too many classes loaded and it is not enough, just adjust the maximum value.
②Old generation
Mainly stores objects in the young generation that survive and are upgraded after multiple recycling cycles. Of course, for some large memory allocations, they may also be allocated directly to the permanent generation (an extreme example is that the young generation cannot be stored at all).
③Young Generation
Most memory allocation and recycling actions occur in the young generation. As shown in the figure below, the young generation is divided into three areas, the original area (Eden) and two small survival areas (Survivor). The two survival areas are divided into From and To according to their functions. The vast majority of objects are allocated in the original area, and objects that survive more than one garbage collection operation are placed in the survival area.
Serial Collector
Single thread performs recycling operation, suspends the execution of all application threads during recycling, in client mode The default collector is forced to be specified through the -XX:+UseSerialGC command line option.
①The recycling algorithm of the young generation (Minor Collection)
Move the surviving objects in the Eden area to the To area. If the To area cannot fit in it, move it directly to the old generation. Move the From area to the The old ones are moved to the To area. If the To area cannot fit in them, they are directly moved to the old generation. The very old ones in the From area are upgraded to the old generation. After the recycling is completed, both the Eden and From areas are empty. At this time, the functions of From and To are interchanged, From becomes To, and To becomes From. Before each round of recycling, To is empty. The selection of the design is copying.
②Recycling algorithm of the old generation (Full Collection)
The recycling of the old generation is divided into three steps, Mark, Sweep, and Compact ). The marking phase marks all living objects, the clearing phase releases all dead objects, and the merging phase merges all living objects into the front part of the old generation, leaving all free segments at the back. The design selection is merging to reduce memory fragmentation.
Parallel Collector
Use multiple threads to perform garbage collection at the same time. In a multi-core environment, you can make full use of CPU resources, reduce recycling time, and increase JVM productivity. Server The default recycler in mode. Like the serial collector, execution of all application threads is suspended during the collection. Forced by the -XX:+UseParallelGC command line option.
① Young generation recycling algorithm (Minor Collection)
Use multiple threads to collect garbage. The algorithm of each thread is the same as the serial collector.
②Recycling algorithm of the old generation (Full Collection)
The old generation is still single-threaded, the same as the serial collector.
Parallel Compacting Collection
The recycling of the young generation and the old 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 collector, execution of all application threads is suspended during the collection. Compared with the parallel collector, the collection time of the old generation is shorter, thus reducing the pause time interval (Pause time). Forced by the –XX:+UseParallelOldGC command line option.
①The collection algorithm of the young generation (Minor Collection)
The same as the parallel collector (Parallel Collector)
②The recycling algorithm of the old generation (Full Collection) )
The old generation is divided into three steps, marking, statistics, and merging. The idea of dividing is used here to divide the old generation into many fixed-size regions. In the marking phase, all surviving objects are divided into N groups (which should be the same as the number of recycling 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), marked as Parallel. In the statistical phase, the survival rate of each region (Region) is counted. In principle, the survival rate in the front is higher. From front to back, find the starting position worthy of merging (regions where the vast majority of objects are alive are not worth merging). In the statistical phase Is serial (single threaded). In the merging phase, based on the information in the statistics phase, multi-threads copy the surviving objects from one region (Region) to another region (Region) in parallel.
Concurrent Mark-Sweep Collector
Also known as Low-latency Collector, it uses various means to make applications The program is suspended for the shortest amount of time. Recycling operations are basically performed concurrently with the application, without merging and copying operations. Specified through the command line -XX:+UseConcMarkSweepGC, you can also specify the incremental recycling mode -XX:+UseConcMarkSweepGC in a single-core or dual-core system. Incremental recycling refers to dividing the recycling operation into multiple fragments, releasing CPU resources to the application after executing one fragment, and continuing to recycle the last result at a certain point in the future. The purpose is also to reduce delays.
①The collection algorithm of the young generation (Minor Collection)
The same as the parallel collector (Parallel Collector)
②The recycling algorithm of the old generation (Full Collection) )
is divided into four steps, initial mark (Initial Mark), concurrent mark (Concurrent Mark), remark (Remark), and concurrent cleanup (Concurrent Sweep). Note in particular that there is no merge operation, so there will be fragmentation.
Initialization phase: Pause the application thread and find all surviving objects. It takes a short time and the recycler uses a single thread.
Concurrent marking phase: The recycler marking operation runs concurrently with the application, and the recycler uses a single thread to mark surviving objects.
Mark again: In the concurrent marking phase, since the application is also running, objects may be added or modified during this process. So pause the application thread again, find all modified objects, and use multi-thread marking.
Concurrent cleaning: The recycler cleaning operation runs concurrently with the application, and the recycler uses a single thread to clean up dead objects.
Java garbage collector performance evaluation tool
①–XX:+PrintGCDetails and –XX:+PrintGCTimeStamps
Garbage collection Start time, duration, free memory of each generation and other information.
②jmap [options] pid
jamp 2043 View the shared objects loaded in the 2043 process. Usually DLL files.
jmap -heap 2043 View 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 classes.
③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 tuning the garbage collector, first list the performance indicators you are concerned about and tell the JVM the performance indicators you are concerned about through the command line. The JVM will automatically tune it. If you are not satisfied, you can specify the garbage collector. OutOfMemory is usually caused by insufficient heap memory. Just adjust the -Xmx1024m and -XX:MaxPermSize=128m command line options.
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more articles related to the summary of methods and principles of Java garbage collector, please pay attention to the PHP Chinese website!