Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Talk about the garbage collector in the JAVA virtual machine

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Preface

The garbage collector of JAVA virtual machine is the scavenger of virtual machine memory, and its existence allows JAVA developers to devote more energy to business research and development. Understanding the garbage collector and making good use of this tool can better ensure the stability of the service. By analyzing the memory model of JAVA virtual machine, this paper introduces the common algorithms and categories of garbage collector, so that the configuration and use of garbage collector is no longer out of reach.

JAVA virtual machine memory model

JAVA virtual machine memory can be divided into: virtual machine stack, local method stack, JAVA heap memory, method area (including runtime constant pool), program counter, direct memory.

Virtual machine stack

The virtual machine stack is thread private and has the same life cycle as the thread. In other words, after a thread is created, the virtual machine allocates an independent stack frame to store local variables, operands, dynamic links, method exits and other information of the thread. When the thread ends, the stack frame will also be reclaimed and cleaned.

Local method stack

The local method stack is a stack frame used during the execution of the native method of the virtual machine.

JAVA heap memory

Heap memory is an area shared by all threads, which is used to store object instances and arrays, belonging to the largest area of memory and the main area of garbage collection. From the perspective of garbage collection, heap memory is often divided into the new generation and the old age.

Method area

The method area is also an area shared by all threads, which is used to store class information, constants, static variables, JIT compiled code and other data loaded by the virtual machine. It can also be a permanent generation.

Program counter

The program counter is private to the thread, and as a line number indicator of the bytecode executed by the current thread, each thread has a program counter that records the execution position of the current thread when the CPU switches threads, so that execution can continue from the current position next time.

Direct memory

This piece does not belong to JAVA virtual machine memory, but it is used frequently and can also be called "out-of-heap memory".

JAVA virtual machine garbage collector

According to the above introduction to the JAVA virtual machine memory region model, we know that the object instances in JAVA programs are stored in JAVA heap memory, so garbage collection is mainly aimed at heap memory. In order to better manage JAVA object instances and combine the survival time of object instances, JAVA virtual machine divides heap memory into new generation and old age, storing newly created objects and long-lived object instances respectively, and adopts the strategy of generation collection to recover the memory of new generation and old era respectively.

Memory allocation and recovery strategy 1, generation collection ideas. According to the life cycle characteristics of the JAVA object, the virtual machine divides the heap memory into the new generation and the old age, and adopts the garbage collection strategy of the new generation and the old era respectively. 2. The Cenozoic is subdivided into Eden region and two Survivor regions (i.e. From region and To region). Most of the new objects are created frequently and have a short survival time. In order to improve the garbage collection efficiency of the new generation area, the newly created objects are stored in the Eden area. When the Eden area is almost full, the virtual machine triggers a Minor GC to move the new generation survival objects to the From area. The original objects in the From area are placed in the To area or the old age according to the survival age, then empty the Eden area and From area, and then move all the objects in the To area to the From area. 3. Large objects directly enter the old age, and the maximum value of the new generation of objects can be configured. If the object exceeds this value, it will directly enter the old age. 4. Before launching Minor GC, we will first determine whether the maximum available contiguous space in the old era is larger than the space occupied by the new generation of objects. If it is less than or not allowed to take risks, a Full GC will be triggered. Garbage collection algorithm (3 basic algorithms) 1. Replication algorithm. For the new generation of garbage collection algorithm. When the new generation Eden area is almost full, copy the Eden area objects to the From area, copy the From area objects to the To area or to the old age according to the survival age, then clear the Eden area and the From area, and then copy the To area objects to the From area. 2. Mark-clear algorithm. The garbage collection algorithm marks the objects that need to be recycled, and after the marking is completed, it can be recycled directly. The garbage collector uses reachability analysis to determine which objects are alive. By setting up a series of GC Roots nodes (including stacks, static properties in the method area, objects referenced by constants, and objects referenced in the local method stack), the garbage collector searches down from such nodes. When the object is not on the reference chain of the GC Root node, it means that the object is unreachable and can be recycled. 3. Marking-finishing algorithm. The garbage collection algorithm marks the objects that need to be reclaimed, moves the surviving objects to one end of memory after marking, and then cleans up the memory outside the end boundary directly. Common garbage collector

Because the garbage collection in the virtual machine is collected from generation to generation, the garbage collection strategies of the new generation and the old era are not the same, so the combination of garbage collectors for the new generation and the old era is generally used.

1 、 Serial GC . The new generation collector uses the replication algorithm for the new generation garbage collection of Client clients and carries out garbage collection for applications with less memory consumption. 2 、 Serial Old GC . Old-era collector, using mark-finishing algorithm, is used for old-age garbage collection on Client clients, and garbage collection is carried out for applications that occupy less memory. 3 、 Parallel Scavenge GC . The new generation collector uses the replication algorithm to collect the new generation memory garbage in parallel. The throughput of the garbage collector can be set, and the throughput can be adjusted automatically. 4 、 Parallel New GC . The new generation collector uses the replication algorithm to collect the new generation memory garbage in parallel. 5 、 Parallel Old GC . The old-age collector uses the mark-finishing algorithm to collect old-age memory garbage in parallel. 6 、 CMS GC . The old era collector uses the mark-clear algorithm to collect the old memory garbage in parallel without sorting out the memory. Because the business thread is not interrupted during garbage collection, it is easy to produce "floating garbage", resulting in Full GC. The memory demarcation task can be triggered by setting parameters. 7 、 G1 GC . Instead of distinguishing between the new generation and the old era, heap memory is regarded as a number of equally divided small areas, and the most idle memory areas are marked and recycled. Suitable for applications with large memory. Configure garbage collection machine parameter 1, UseSerialGC: the virtual machine allows the default value in Client mode. After this configuration is turned on, the virtual machine uses the collector combination of SerialGC + Serial Old GC for memory collection. 2. UseParNewGc: use the collector combination of ParNew + Serial Old for memory recovery. 3. UseConcMarkSweepGC: use the collector combination of ParNew + CMS + Serial Old GC for memory recovery. 4. UseParallelGC: the virtual machine allows the default value in Server mode and uses the collector combination of Parallel Scavenge + Serial Old for memory recovery. 5. UseParallelOldGC: use the collector combination of Parallel Scavenge + ParallelOldGC for memory recovery. 6. SuriviorRatio: the capacity ratio of the Eden region to the Surivior region in the Cenozoic. The default is 8:1. 7. PretenureSizeThreshold: after setting this value, objects larger than this value go directly into the old age. 8. MaxtenuringThreshold: enter the old age when the age of the object exceeds this value. 9. ParallelGCThreads: set the number of threads for memory collection when parallel GC. 10. UseAdaptiveSizePolicy: dynamically adjust the size of each area of heap memory and the age of objects entering the old age. 11. HandlerPromotionFailure: failure to allow the allocation of guarantees. 12. GCTimeRatio: effective only when Parallel ScaVenge Collector. Set the ratio of GC time to total elapsed time. Default is 1%. 13. MaxGCPauseMills: it only takes effect when the Parallel ScaVenge collector is used. Set the maximum pause time for GC. 14. CMSInitiatingOccupancyFraction: sets the amount of space used by the CMS collector to trigger garbage collection. The default is 68%, which takes effect when setting the CMS collector. 15. UseCMSCompactAtFullCollection: takes effect when setting the CMS collector, setting whether the CMS collector will defragment after completing the garbage collection. 16. CMSFullGCsBeforeCompaction: effective only when using CMS, setting the CMS collector to start memory defragmentation after several collections.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report