In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how JVM's garbage collection algorithm works". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how JVM's garbage collection algorithm works.
How to determine whether the object can be recycled?
There are two methods, citation counting and reachability analysis.
1. Citation counting method
The so-called reference counting method is to set a reference counter for each object. whenever there is a place to reference the object, the counter is increased by one, and when the reference expires, the counter is reduced by one. When an object's reference counter is 00:00, the object is not referenced, that is, a "dead object" and will be garbage collected.
A defect of reference counting method is that it can not solve the problem of circular reference, that is to say, when object A refers to object B, and object B refers to object A, then the reference counter of AMagi B object is not zero, which makes it impossible to complete garbage collection, so the mainstream virtual machines do not adopt this algorithm.
Public classReferenceFindTest {publicstaticvoidmain (String [] args) {MyObject object1 = new MyObject (); MyObject object2 = new MyObject (); object1.object = object2;object2.object = object1;object1 = null;object2 = null;}}
two。 Reachability algorithm (reference chain method)
The idea of the algorithm is to search down from an object called GC Roots, and if an object is not linked to GC Roots with any references, the object is not available.
There are several kinds of objects that can be used as GC Roots in java:
Object method area referenced in virtual machine stack object referenced by class static attribute object referenced by constant pool object local method stack referenced object referenced by JNI
Although these algorithms can determine whether an object can be recycled, an object ratio may not be recycled when the above conditions are met. When an object is not reachable to GC Root, the object is not immediately reclaimed, but is in a reprieve phase, and it needs to be marked twice to be actually recycled.
If the object does not have a reference chain with GC Root in the reachability analysis, it is marked for the first time and filtered if it is necessary to execute the finalize () method. When the object does not override the finalize () method or has been called by the virtual machine, it is considered unnecessary.
If it is necessary for the object to execute the finalize () method, the object will be placed in a pair queue called F-Queue, and the virtual machine will trigger a Finalize () thread to execute, which is a low priority thread, and the virtual machine will not promise to wait for it to finish, because if the execution of finalize () is slow or deadlock occurs, it will cause the F-Queue queue to wait. Caused the crash of the memory recovery system. GC marks an object in F-Queue for the second time, at which point the object is removed from the "about to recycle" collection, waiting for recycling.
Heap memory generation strategy and its significance
strategy
Java virtual machine divides heap memory into new generation, old war and permanent generation, permanent generation is a unique concept of HotSpaot virtual machine, it uses permanent generation to implement method area, other virtual machine implementations do not have this concept, and HotSpot also has a tendency to cancel permanent generation. In JDK 1.7, HotSpot has begun to "de-perpetuate" and remove the string constant pool originally placed in the permanent generation. Permanent generation mainly stores constant, class information, static variables and other data (transplanted to the method area), which has little to do with garbage collection. The new generation and the old era are the main areas of garbage collection.
New generation (Young)
The newly generated objects are preferentially stored in the new generation, which dies overnight, and the survival rate is very low. in the new generation, conventional applications can recover 70% to 95% of the space in a garbage collection, and the recovery efficiency is very high.
Old age (OldGenerationn)
Objects that survive many times in the new generation (depending on the threshold of the virtual machine configuration) GC will enter the old age. In the old age, the life cycle of the object is longer, the survival rate is higher, the frequency of GC in the old age is relatively low, and the speed of recovery is relatively slow.
Permanent generation (PermanentGenerationn)
Permanent storage of class information, constants, static variables, code compiled by the just-in-time compiler, and so on. For this area, the Java virtual machine specification states that there can be no garbage collection, and generally there will be no garbage collection.
Jdk1.6 and before: there is a permanent generation, constant pool 1.6 in the method area. Jdk1.7: there is a permanent generation, but it has been gradually "de-permanent". The constant pool is 1.7 in the heap. Jdk1.8 and later: no permanent generation, constant pool 1.8 in metaspace. The metaspace is directly stored in memory, not in the java virtual machine, so metaspace depends on the size of memory. Of course, you can also customize the metaspace size.
Meaning
With memory generation, newly created objects will allocate memory in the new generation, objects that survive many times of recycling will be stored in the old era, static properties and class information will be stored in the permanent generation, objects in the new generation will survive for a short time, and only need to be frequently GC in the new generation. In the old era, the life cycle of the object is long, and the frequency of memory recovery is relatively low, so there is no need for frequent recycling. The effect of recycling in the permanent generation is too poor, generally do not carry out garbage collection, and different garbage collection algorithms can be adopted according to the characteristics of different ages. Generational garbage collection greatly improves the efficiency of garbage collection, which are the benefits of JVM generation.
Garbage collection algorithm
1. Replication algorithm
The replication algorithm divides the available memory into two equal parts according to capacity, and then uses only one of them at a time. When one piece of memory is used up, the surviving objects are copied to the second block of memory, and then the first block of memory is clear at one time. Then copy the objects on the second block to the first block. But in this way, the cost of memory is too high, basically wasting half of the memory each time.
two。 Label removal algorithm
Is the oldest JVM garbage collection algorithm, the algorithm is divided into two phases, the first stage starts from the reference root node to mark all the referenced objects, the second stage traverses the entire heap to clear the untagged objects. The disadvantage of this algorithm is that the entire application needs to be paused, and the unused space after recycling is discontinuous, that is, memory fragments, which will affect storage.
3. Tag finishing algorithm
This algorithm combines the advantages of the tag-clear algorithm and the replication algorithm, and is also divided into two stages. the first stage marks all the referenced objects from the reference root node, and the second stage traverses the whole heap. After reclaiming the space occupied by the non-living objects, all the living objects will be moved to the left free space and the corresponding pointers will be updated. The marking-sorting algorithm is based on the marking-clearing algorithm, and moves the objects, so the cost is higher, but it solves the problem of memory fragmentation, which is discharged sequentially, and solves the problem that the memory space required by the replication algorithm is too large.
4. Generational collection
Generation collection algorithm is the algorithm adopted by most JVM garbage collectors at present. Its core idea is to divide the memory into several different regions according to the life cycle of the object. Generally speaking, the reactor area is divided into Tenured Generation and Young Generation, and another generation outside the reactor area is Permanet Generation. The characteristic of the old era is that only a small number of objects need to be collected during each garbage collection, while the characteristic of the new generation is that there are a large number of objects that need to be collected each time, so we can adopt the most suitable collection algorithm according to the characteristics of different generations.
a. Young generation recycling algorithm (the core is actually the replication algorithm)
HotSpot divides the Cenozoic into three blocks, a larger Eden space and two smaller Survivor spaces, with a default ratio of 8: 1: 1. The purpose of the partition is because HotSpot uses the replication algorithm to recycle the new generation, and this ratio is set to make full use of memory space and reduce waste. The newly generated objects are allocated in the Eden area (except for large objects, which go directly into the old age), and when there is not enough space in the Eden area to allocate, the virtual machine will initiate a Minor GC. At the beginning of GC, objects only exist in the Eden zone and the From Survivor zone, and the To Survivor zone is empty (as a reserved area).
When GC is in progress, all living objects in the Eden area will be copied to the To Survivor area, while in the FromSurvivor area, the surviving objects will go according to their age value, and the age value will reach the threshold (the default is 15, the object in the new generation will add 1 for each round of garbage collection, and the GC generation age will be stored in the object's header). Objects that do not reach the threshold will be copied to the To Survivor area.
Then clear the Eden area and the From Survivor area, and all the surviving objects in the Cenozoic era are in the To Survivor area. Then, the FromSurvivor area and the To Survivor zone exchange their roles, that is, the new To Survivor area is the FromSurvivor area that was emptied by the last GC, and the new FromSurvivor zone is. In short, the To Survivor area of the last GC ensures that the To Survivor area is empty after a round of GC (in fact, this is the younger generation collection algorithm in the generational collection algorithm, which we will see later).
In GC, when the To Survivor area does not have enough space to store the surviving objects collected by the last Cenozoic generation, it needs to rely on the old age for allocation guarantee to store these objects in the old age.
b. Old-age recovery algorithm (recovery is mainly based on marking-finishing)
1) objects that are still alive after N garbage collections in the younger generation will be put into the older generation. Therefore, it can be considered that the objects stored in the elderly are objects with a longer life cycle.
2) the memory is also much larger than that of the new generation (about 1:2). When the memory is full, the frequency of triggering Major GC, that is, Full GC,Full GC is relatively low, the survival time of the old object is longer, and the survival rate mark is high.
c. Recovery algorithm of persistent Generation (Permanent Generation)
Used to store static files, such as Java classes, methods, etc. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some class, such as Hibernate, at this time, you need to set a large persistent generation space to store these new classes. Garbage collection is rare in this area, but that doesn't mean GC doesn't happen. The GC here is mainly about constant pools and type unloading in persistent generations.
Conditions:
1) all instances of this class have been recycled, that is, no instances of this class exist in the Java heap
2) the ClassLoader loaded with this class has been recycled
3) the corresponding java.lang.Class object of this class is not referenced anywhere, and the method of this class cannot be accessed by reflection anywhere.
The virtual machine can recycle the useless classes that meet the above three conditions. Here, it is only "can", not like objects. If it is not used, it is bound to be recycled!
At this point, I believe that everyone on the "JVM garbage collection algorithm working principle" have a deeper understanding, might as well to the actual operation of it! 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.