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 principle of GC garbage collection in JVM?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What this article shares with you is about the principle of GC garbage collection in JVM. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Principle of GC garbage collection

1 how to judge that the object is garbage?

There are two classic methods of judgment, borrowing the pictures of netizens (a link is given at the end of the article):

The idea of citation counting is very simple, but if there is a circular reference, that is, A quotes B and A, it is not easy to do so, so another judgment method called "reachability analysis" is used in JVM:

Or is it the circular reference question just now (which some company interviewers may ask). If A references B and A, can these two objects be recycled by GC?

Answer: the key is not whether there is a reference between An and B, but whether An and B can be traced all the way up to GC Roots. If it is not associated with GC Roots, it will be recycled, otherwise it will continue to survive.

The image above is an example of marking junk objects with "reachability analysis". The gray objects represent unreachable objects and will wait for recycling.

2 which memory areas need GC?

In the first part of the JVM memory layout, we know that the exclusive areas of thread: PC Regiester, JVM Stack, Native Method Stack, all have the same life cycle as threads (that is, live and die with threads), so there is no need for GC. Heap area and Method Area shared by threads are the key objects that GC pays close attention to.

3 commonly used GC algorithm

1) mark-sweep Marker removal

As shown in the image above, the black area represents the garbage object to be cleaned up, which is marked and emptied directly. This method is simple and fast, but the disadvantage is also obvious, which will produce a lot of memory fragments.

2) mark-copy marker replication method

The idea is also simple: divide the memory in half, always leave a piece empty (on the right in the image above), copy the surviving objects on the left (light gray area) to the right, and then empty all on the left. The memory fragmentation problem is avoided, but the memory waste is so serious that only 50% of the memory can be used.

3) mark-compact marking-finishing (also known as marking-compression) method

Avoid the shortcomings of the above two algorithms, clean up the garbage objects and move the remaining surviving objects (similar to windows disk defragmentation) to ensure that they occupy continuous space, so as to avoid the problem of memory fragmentation, but the sorting process will also reduce the efficiency of GC.

4) generation-collect generation collection algorithm

The above three algorithms, each has its own advantages and disadvantages, are not perfect. In modern JVM, it is often used comprehensively. After a lot of practical analysis, it is found that the objects in memory can be roughly divided into two categories: some have a short life cycle, such as some local variables / temporary objects, while others will survive for a long time, such as connection objects in websocket persistent connections, as shown below:

The vertical y-axis can understand the number of bytes allocated to memory, while the horizontal x-axis means that over time (along with GC), you can find that most objects are actually quite short-lived, and few objects survive after GC. So the idea of generational generation was born. Take Hotspot as an example (JDK 7):

The memory is divided into three blocks: Young Genaration, Old Generation, and Permanent Generation, of which Young Genaration is divided into three eden,S0,S1 regions.

Combined with some jvm tuning parameters that we often use, the memory size of each region can be affected by some parameters, as shown below:

Note: starting with jdk8, the Perm area is replaced by the MetaSpace area (permanent generation), so the corresponding jvm parameters become-XX:MetaspaceSize and-XX:MaxMetaspaceSize.

Taking Hotspot as an example, let's analyze the main process of GC:

At first, the objects were allocated in the eden area, S0 (that is, from) and S1 (that is, to), almost empty.

With the running of the application, more and more objects are assigned to the eden area.

When the eden area does not fit, minor GC (also known as young GC) occurs. The first step is of course to identify the unreachable garbage object (that is, the yellow block in the following image), then move the reachable object to S0 area (that is, 4 light blue squares to S0 area), and then clean up the yellow garbage block. After this round, the eden area becomes empty.

Note: in fact, the "[tag-clean eden] + [tag-copy eden- > S0] algorithm has been used here.

With the passage of time, if eden is full again and minor GC is triggered again, it is also necessary to mark it first. There may be junk objects in both eden and S0 (the yellow block in the following figure). Note: S1 (i.e., to) is empty, and the living objects in S0 and eden will be moved directly to S1. Then clean up the garbage in eden and S0, and after this round of minor GC, eden and S0 become empty.

Continue, with the continuous allocation of objects, the eden may be empty again, and the minor GC process will be repeated, but it should be noted that S0 is empty at this time, so the roles of S0 and S1 will actually switch, that is, the surviving objects will move from eden and S1 to S0. Then remove the garbage from eden and S1, and after this round, eden and S1 become empty, as shown in the following figure.

For those who compare "longevity", they have been moving back and forth between S0 and S1, which not only takes up a lot of space, but also causes some expenses and reduces the efficiency of gc, so there is "age" and "promotion".

The subjects moved from one district to another district each time between the three districts (edge,s0,s1) of the young generation, and the age was + 1. When the young area reached a certain age threshold, it would be promoted to the old age. The figure below is 8, that is, if you are still alive after moving 8 times, the next time you minor GC, you will move to the Tenured area.

The following is the main process of promotion: the object is first assigned to the younger generation, and after many times of Young GC, if the object is still alive, it is promoted to the old age.

If the old age is finally full, major GC (that is, Full GC) will occur, because there are usually more objects in the old era, because marking-cleaning-finishing (compression) usually takes a long time, which makes applications stutter, which is why many applications need to be optimized to avoid or reduce Full GC as much as possible.

Note: the above process is mainly from the oracle official website, but there is a detail not mentioned on the official website: if the new object assigned is relatively large, the eden area cannot be put down, but when the old area can be put down, it will be directly assigned to the old area (that is, there is no promotion process, directly to the old age).

The following picture is quoted from Ali's book "Code out efficiency-Java Development Manual", which combs the main process of GC.

These are the principles of GC garbage collection in JVM. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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