In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the garbage collection mechanism of Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the garbage collection mechanism of Java"!
What is automatic garbage collection?
Automatic garbage collection is a mechanism to find out which objects are in use and which are not in use in heap memory and delete the latter.
The so-called object in use (referenced object) refers to the object pointed to by the pointer in the program, while the unused object (unreferenced object) is not pointed to by any pointer, so the occupied memory can also be reclaimed.
When using a programming language such as C, programmers need to allocate and free memory manually. Unlike Java, it has a garbage collector, which is responsible for releasing memory. Next, this article will introduce the basic process of the garbage collection mechanism.
Step 1: the first step in tagging garbage collection is tagging. At this point, the garbage collector finds out which memory is in use and which is not.
In the image above, blue indicates the referenced object and orange indicates the unreferenced object. The garbage collector has to check all the objects to know which ones are referenced and which ones are not. If all objects in the system have to be checked, this step can be quite time-consuming. Follow Java technology stack Wechat official account, reply: JVM46, you can get a super full JVM tuning strategy.
The memory allocator retains references to available memory for allocating new objects.
Compression to improve performance, after you delete unreferenced objects, you can also put the remaining referenced objects together (compress), so that new objects can be allocated more easily and quickly.
Why do you need generation-by-generation garbage collection? As mentioned earlier, tagging and compressing all the objects in the Java virtual machine one by one is very inefficient: the more objects are allocated, the longer it takes for garbage collection. However, according to statistics, most of the objects will not be used for long.
Let's look at an example. (in the following figure, the vertical axis represents the allocated bytes, while the horizontal axis represents the running time of the program)
As you can see in the image above, there are fewer and fewer objects that survive (not released) over time. The peaks on the left side of the picture also show that most objects are actually quite short-lived.
According to the previous law, JVM generation can be used to improve the efficiency of JVM. The method is to divide the heap into several parts (so-called generations), namely, the new generation, the old age, and the immortal generation.
The new object will be allocated in the new generation of memory. Once the new generation of memory is full, it will begin the so-called small garbage collection process for dead objects. In a new generation of memory, the more dead you are, the faster the recycling process will be; as for those objects that are still alive, they will age and eventually enter the old memory.
Stop the World event-small garbage collection belongs to an event called "Stop the World". When such an event occurs, all program threads are paused until the event is complete (for example, in this case, all recycling work is done).
In the old days, it was used to preserve objects that had survived for a long time. Usually, a threshold is set so that when the age is reached, the younger generation will be moved to the old age. Eventually, the old age will also be recycled. This event became Major GC.
Major GC also triggers STW (Stop the World). In general, Major GC is much slower because it involves all living objects. Therefore, for responsive applications, Major GC should be avoided as much as possible. Also note that the length of Major GC's STW is affected by the type of older garbage collector.
The permanent generation contains the metadata that JVM uses to describe classes and methods in the application. The permanent generation is populated by JVM at run time based on the classes used by the application. In addition, the Java SE class libraries and methods are also stored here.
If JVM finds that some classes are no longer needed and other classes may need space, they may be recycled.
Generational garbage collection process
Now that you understand why the heap is divided into different generations, it's time to look at how these spaces interact. The following picture describes the object allocation and aging process in JVM. Follow Java technology stack Wechat official account, reply: JVM46, you can get a super full JVM tuning strategy.
First, assign any new objects to the eden space. Both survivor spaces are empty.
A slight garbage collection is triggered when the eden space fills up.
The referenced object is moved to the first survivor space. When you clear eden space, unreferenced objects are deleted.
In the next Minor GC, the Eden area will do the same. Delete the unreferenced object and move the referenced object to the Survivor area. Here, however, they are moved to the second Survivor area (S1).
In addition, in the first Survivor area (S0), the objects that survived the last Minor GC increase their age and are moved to S1. After all the surviving objects are moved to S1, the S0 and Eden areas are cleared. Notice that there are objects of different ages in the Survivor area.
The same operation will be repeated in the next Minor GC. However, this time the Survivor zone will be swapped. The referenced object is moved to S0. Those who survived increased their age. The Eden area and S1 are emptied.
This slide demonstrates promotion. After a smaller GC, when aging objects reach a certain age threshold (8 in this example), they rise from the younger generation to the older generation.
As smaller GC continues to occur, objects will continue to be extended to older generations of space.
So it almost covers the whole process of the younger generation. Eventually, GC will be performed mainly on the older generation, cleaning up and eventually compressing the space.
At this point, I believe you have a deeper understanding of "what is the garbage collection mechanism of Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.