What is the Java GC recycling mechanism?

1. How to define garbage

        As a mainstream programming language, Java is also inseparable from its GC recycling mechanism. Compared with C, you need to open up space and reclaim space by yourself. Java has greatly improved development efficiency, so that programmers do not have to focus on things other than code.

        So how do we define whether an object is "garbage"?

        The earliest words used a counting method. That is, there is something like a counter in the heap to determine whether the objects in the heap are still referenced by anyone. If the counter is cleared, it is judged that the object is useless and can be recycled and cleared.

         The counter method is similar to this. I heard from other bigwigs that python still uses this recycling mechanism. But Java may have mutual references between objects, and this method will not work.

         For example, the following picture:

 Maybe the metaphor is not very appropriate, but the truth is like this.

        Later, after realizing this problem, our root-finding method appeared. According to the object, we searched all the way down to find out whether there is a stack object reference at the bottom layer.

2. Three mainstream recycling algorithms for GC today

        In the future, our Garden of Eden and the old age are also based on these three types.

        1. Mark removal

        This is the most basic recycling algorithm. It first finds useless "garbage objects" through the root-finding method, and then marks them. Then in the clear phase. to recycle.

        Disadvantages: Although this algorithm is simple. But it will lead to fragmented and sparse memory space. As a result, when we store large objects again, the garbage collection mechanism will be triggered. As shown in the picture:

         2. copying algorithm

        This algorithm is simple and efficient. And will not generate memory fragmentation. But it is in exchange for a huge memory space. Because the memory space we can use is only half of the copying area. There may be some words in the copying area that do not convey the meaning. Let's say he is the Survivor space in the new generation.

        It splits our memory space in half, using only half of it at a time. The recycling mechanism is to directly transfer useful objects to the other half, and then clear this half of the memory space (also garbage objects).

         3. Label arrangement

               This algorithm is similar to the label deletion algorithm, but before the garbage objects are cleared, the useful objects will be moved to one end. The garbage collection mechanism is completed, and a continuous and clean memory space is preserved.

        Disadvantages: It consumes more computing power, and is not very friendly to CPU and time.

 

        Finally, the JVM garbage collection mechanism integrates the above three algorithms. A generational algorithm was created.

3. Generation Algorithm (Garden of Eden New Generation + Old Generation)

        The advantage of the generational algorithm is that it performs different garbage collection algorithms according to the characteristics of each generational area.

       When we create a new object (excluding large objects: such as large strings, etc.), it will be allocated to our new generation. Because the new generation object update iteration is faster. So the faster mark removal and copying algorithm is used. The objects in the old generation are large objects and commonly used objects. So the gc frequency will be much lower. Just use the tagging algorithm. It will neither waste performance too much, but also ensure the continuous integrity of memory, can store more objects, and will not trigger gc frequently.

         This is the approximate memory space allocation of the generational algorithm. Let's now simulate a small object (object01), under the condition of being used all the time, the activity track of the memory space being allocated.

         The Survivor area uses the copying algorithm. It also adds an age mechanic. When your object has survived many times in the Survivor area, it is determined that your object is a commonly used object that does not need to be released. It will be thrown to the old generation for you to manage.

Fourth, the version iteration of the garbage collector

        The earliest garbage collectors were single-threaded. When we perform the garbage collection mechanism, all threads and codes have to stop. That is STW (stop the world: the world stops)

        It's also multi-threaded now. The actual default is based on your own JDK version. You can also modify and choose the garbage collector yourself.

It is also in JVM tuning.

Extended Discussion:

        JVM tuning is a relatively high-frequency interview, and it is also written on the recruitment information of the employer. For example: Do you have jvm tuning experience? Those who have jvm tuning are preferred. But I haven't felt that I need to manually modify the JVM configuration for such a long time. It is nothing more than allocating space according to the situation of the server when the project is deployed.

        It feels like if the code is tuned (less unnecessary objects are created, less large objects are created), then the framework is tuned. It is really rare to encounter the need for JVM tuning. It may be that the companies I joined are relatively small. There are useful and commonly used jvm tuning, you can comment @我下.

Guess you like

Origin blog.csdn.net/m0_58907154/article/details/130507245