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

What is the underlying principle of Java garbage collection mechanism?

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

Share

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

This article introduces you what is the underlying principle of Java garbage collection mechanism, the content is very detailed, interested friends can refer to, hope to be helpful to you.

3.1.2 garbage collection mechanism

The program counter, virtual machine stack and local method stack in the runtime area of Java memory rise and die with the thread, and the stack frames in the stack methodically execute the operation of unstack and stack with the entry and exit of the method. How much memory is allocated in each stack frame is basically known when the class structure is determined (although some optimizations will be made by the JIT compiler at run time), so memory allocation and recycling in these areas are deterministic, and there is no need to think too much about recycling, because when the method ends or the thread ends, the memory naturally follows the collection.

While the Java heap is different, multiple implementation classes in an interface may need different memory, and multiple branches of a method may need different memory. Only when the program is running can we know which objects will be created. The allocation and collection of this part of memory are dynamic, and this is the memory that the garbage collector focuses on.

Automatic garbage collection

Automatic garbage collection is the process of looking at heap memory, identifying which objects are being used, which objects are not deleted, and deleting unused objects. An object in use or a referenced object means that some parts of the program still maintain a pointer to that object. If any part of the program no longer references unused or unreferenced objects, you can reclaim the memory of unreferenced objects. In C language, memory allocation and release is a manual process, and memory allocation and collection in Java language is handled automatically by garbage collector.

In automatic garbage collection, the first step in determining which memory needs to be reclaimed is marking, using reference counting, reachability analysis, etc., to mark which memory is in use and which memory is not.

Citation counting method

The reference counting method is mainly by adding a reference counter to the object. Whenever a place references it, the value of the counter is increased by 1. When the reference expires, the value of the counter is reduced by 1. When the value of the counter is 0, the object cannot be used and can be garbage collected. The implementation of reference counting method is simple and efficient, but it is difficult to solve the problem of circular reference between objects.

Reachability analysis algorithm

Accessibility analysis algorithm, also known as root search algorithm, the basic idea of this algorithm is through a series of objects called "GC Roots" as the starting point, search down from these nodes, the search path is called reference chain, when an object to "GC Roots" does not have any reference chain, then it is proved that the object is unavailable and can be garbage collected.

To put it simply, think of the object and its reference relationship as a graph, select the active object as "GC Roots", and then track the reference chain. If an object and "GC Roots" is unreachable, that is, there is no reference, it can be considered a recyclable object.

Reference type and accessibility level

Reference type:

Strong Reference: the most common common reference, as long as there is a strong reference to an object, will not be recycled

Soft reference (Soft Reference): when JVM thinks there is not enough memory, he will try to recycle the object pointed to by the soft reference (cache scene)

Weak Reference: although it is a reference, it may be recycled at any time

Virtual reference (Phantom Reference): cannot access an object through it, providing a mechanism for executing specified logic after the object is finalize.

Level of accessibility:

Strongly Reachable: a situation in which an object can be accessed by one or more threads without various references

Soft Softly Reachable: a state that can only be accessed through a soft reference

Weak reference (Weakly Reachable): the state that can only be accessed through a weak reference. When a weak reference is cleared, the destroy condition is met.

Phantom Phantom Reachable: there are no other references, and the finalize has been passed, only the phantom reference points to this object

Unreachable (Unreachable): means that objects can be cleared

Garbage collection algorithm

At present, the mainstream and widely used garbage collection algorithms mainly include tag-removal algorithm (Mark-Sweep), replication algorithm (Coping) and tag-collation algorithm (Mark-Compact).

Mark-clear algorithm: first identify all the objects to be recycled, and then clear them. Marking, the efficiency of the cleaning process is limited, there is a problem of memory fragmentation, which is not suitable for particularly large heaps. Other collection algorithms are basically improved based on the idea of tag-cleanup.

Replication algorithm: divide two areas of the same size, and copy the living objects to another area during collection. By placing objects sequentially during replication, memory fragmentation can be avoided. But copying plus reserved memory is a waste.

Tag-collation algorithm: similar to mark-cleanup, but to avoid memory fragmentation, it moves objects during cleanup to ensure that moving objects occupy continuous memory

Generational collection

According to the survival period of the object, the memory is divided into several regions, and the appropriate garbage collection algorithm is used in different areas. At present, the mainstream JVM generally divides heap memory into the new generation and the old age (the size ratio is listed as 1:2), and the new generation is divided into eden, from survivor, and to survivor (the size ratio is listed as 8:1:1). In the new generation, each garbage collection found that a large number of objects died, only a small number of survival, then we can choose the replication algorithm, only pay a small amount of object replication cost to complete the collection. In the old days, when the survival rate of objects was high, the mark-clear, mark-organize algorithm could be used.

The new generation is the place where almost all JAVA objects are born, and the memory and storage of JAVA objects are all in this place. JVM will only use the eden and a piece of survivor in the new generation to serve the objects every time, so there will be a piece of survivor space free at any time. When the object is still alive after a minor gc and can be accommodated by another survivor, use the replication algorithm to copy the surviving object into another survior area, then clean up the eden and the previously used survivor area, and make the surviving object age + 1, and then increase the age of the object by 1 per minor gc in the survivor, when the age reaches a certain value (default 15 By setting the parameter-XX:MaxTenuringThreshold to set), these objects will enter the old age. Of course, for some larger objects can directly enter the old age, you can set the threshold for large objects to enter the old age according to-XX:+PretenureSizeThreshold.

Garbage collector

Garbage collection algorithm is only the theoretical method of memory collection, and garbage collector is the concrete implementation of memory collection. There is no specific provision for the implementation of the garbage collector in the Java virtual machine specification, and different manufacturers and different versions of the garbage collector may vary greatly. At present, the common garbage collectors mainly include Serial collector, ParNew collector, Parallel Scavenge collector, Serial Old collector, Parallel Old collector, CMS collector and G1 collector. These garbage collectors have their own advantages and disadvantages and are generally used together. The following figure shows the combinations of garbage collectors available in the new and old generations:

Serial collector

The Serial collector is a serial collector that uses a single thread to perform all garbage collection work, which is suitable for single-processor machines. GC pauses all other worker threads, namely "Stop The World" while it is working. Serial collector is a new generation of single-threaded collector, which uses replication algorithm and is the default new generation collector for virtual machines in client mode. It can be used by setting the parameter-XX:+UseSerialGC.

ParNew collector

The ParNew collector can be understood as the multithreaded version of the serial collector, and its overall algorithm is similar to that of Serial, except that it uses multithreading for garbage collection, and the rest is basically the same as the Serial collector. The ParNew collector is the default new generation collector for virtual machines in server mode, which can be used by setting the parameter-XX:+UseParNewGC. By default, it has the same number of threads as CPU, and you can configure the number of threads for garbage collection by setting the parameter-XX:ParallelGCThreads.

Parallel Scavenge collector

The Parallel Scavenge collector is also a new generation of parallel garbage collector that uses replication algorithms. The goal of the Parallel Scavenge collector is to achieve a controllable throughput. Throughput is the ratio of the time it takes CPU to run user code to the total time consumed by CPU, that is, throughput = time to run user code / (time to run user code + garbage collection time). The Parallel Scavenge collector provides-XX:MaxGCPauseMillis to control the maximum garbage collection pause time and-XX:GCTimeRatio to set the throughput directly.

Serial Old collector

The SerialOld collector is an old version of the Serial collector that uses the tag-collation algorithm and can be used by setting the parameter-XX:+UseSerialOldGC.

Parallel Old collector

The ParallelOld collector is an old version of the Parallel Scavenge collector that uses multithreading and mark-up algorithms and can be used by setting the parameter-XX:+UseParallelOldGC.

CMS collector

The CMS collector is a concurrent collector, CMS or Concurrent Mark Swap, mainly used in old times, using the mark-clear algorithm, which can be used by setting the parameter-XX:+UseConcMarkSweepGC. The CMS collector can execute user programs and garbage collection in parallel, thus reducing the pause time for collection. The main purpose of the design of the CMS collector is to reduce the recovery pause time. At present, many Java applications are concentrated on the Internet website or the server side of the Bamp S system. This kind of application pays special attention to the response speed of the service, hoping that the system pause time is the shortest, and the CMS collector meets the requirements of this kind of application very well. Of course, the CMS collector also has its own shortcomings, it will occupy more CPU resources and compete with user threads, at the same time, due to the use of mark-clear algorithm, there is the problem of memory fragmentation, full gc may cause bad pauses when running for a long time.

The whole process of CMS collector garbage collection consists of four steps: initial marking, concurrent tagging, rewriting tagging, and concurrent cleanup. Initial markup and rewriting markup still require "Stop The World", but the speed is very fast, and the time-consuming operations throughout the process are in the concurrent marking and concurrent clearing phase, during which the collector thread can work with the user thread.

G1 collector

G1 (Garage First) collector is the most cutting-edge achievement of current garbage collector technology. It is used in JDK7 (can be used through configuration-XX:+UseG1GC). Since JDK9, G1 has become the default garbage collector for JVM. The G1 garbage collector is also a server-side garbage collector that focuses on latency, and its goal is to replace the CMS garbage collector.

G1 adopts the idea of Region, which divides the whole heap space into several memory areas of the same size, and each allocation of object space will use memory segment by segment. The biggest advantage of this is that it is divided into parts and avoids full memory scanning, which only needs to be scanned according to the area. The partition size (1MB~32MB, which must be a power of 2) can be specified with the parameter-XX:G1HeapRegionSize=n at startup, and the whole heap is divided into 2048 partitions by default. In the use of heap, there is no physical difference between the younger generation and the old generation in the entire memory partition, and there is no need for a completely separate Survivor to space heap to prepare for replication. G1 only has the concept of logical generation, or each partition may switch back and forth between generations as G1 runs.

The G1 collector adopts the mark-demarcation algorithm as a whole, and partly through the replication algorithm, it will not produce memory fragmentation, so it can make full use of the hardware advantages of multi-CPU and multi-core environment and shorten the time of "Stop The World" as much as possible.

The left side of the following figure is an overview of the memory partition of the G1 collector, which is Cenozoic in red (eden without S, survivor with S) and old in blue (humongous with H, storage of humongous objects, that is, objects greater than or equal to half of region). On the right is the garbage collection ring diagram of the G1 collector. G1 garbage collection can be divided into two stages at the level, young-only and Space-reclamation, and Initial Mark and Remark,Cleanup in young-only.

On the underlying principles of Java garbage collection mechanism is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 0

*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