In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about the example analysis of Java virtual machine garbage collection, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
First, how to determine that the object is a garbage object-verbose:gc print garbage collection simple information parameters
-xx:+PringDCDetail prints garbage collection details
Citation counting method
The reference counting algorithm is simple. It actually saves the number of times the object is referenced by allocating a space in the object header. If the object is referenced by another object, its reference count is increased by one, if the reference to the object is deleted, its reference count is reduced by one, and when the object's reference count is 0, the object is recycled.
In the garbage collection mechanism with reference counting, the overhead of garbage collection is apportioned to the running of the entire application, instead of suspending the running of the entire application until the processing of all objects in the heap is finished. Therefore, garbage collection with reference counting does not belong to the strict "Stop-The-World" garbage collection mechanism.
A big problem with the reference counting algorithm is that it cannot handle circular data, that is, if two objects refer to each other, the two objects cannot be recycled because their reference count is always 1. This is what we often call a memory leak.
The strategy adopted in Python is mainly based on the reference counting mechanism and supplemented by two mechanisms: marking-clearing and generational collection.
Accessibility analysis method
In order to solve the problem that the garbage will not be collected due to the circular reference in the reference counting algorithm, the reachability analysis method is adopted in Java. Also using this method are C# and Lisp (the earliest language to use dynamic memory allocation). The basic idea of this method is to search through a series of "GC Roots" objects as a starting point. If there is no reachable path between "GC Roots" and an object, the object is said to be unreachable, but it should be noted that the object that is determined to be unreachable does not necessarily become a recyclable object. An object that is determined to be unreachable must go through at least two marking processes in order to become a recyclable object, and if it still does not escape the possibility of becoming a recyclable object during these two marking processes, it will basically become a recyclable object.
Objects of GC Roots
The object referenced in the virtual machine stack (the local variable table in the stack frame). (it can be understood as: all objects that reference the local variable table in the stack frame)
The object referenced by the static property in the method area (it can be understood that all objects referencing the static property in the method area)
Objects referenced by constants in the method area (it can be understood as: all objects referencing constants in the method area)
Objects referenced in the local method stack (Native method) (it can be understood that all objects that reference the Native method)
Second, how to reclaim the memory generation division of JVM
The Java virtual machine divides heap memory into the new generation, the old age, and the permanent generation.
New generation (Young)
HotSpot divides the Cenozoic into three pieces, 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.
Old age (Old)
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 (Permanent)
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.
Recycling strategy 1. Mark-clear algorithm (Mark-Sweep)
The algorithm is divided into two stages: "marking" and "clearing": first, all the objects that need to be recycled are marked, and all the marked objects are recycled uniformly after the marking is completed.
The main drawbacks of the tag-clear algorithm:
Efficiency issues: the labeling and removal processes are inefficient
Space problem: a large number of discontiguous memory fragments will be generated after mark removal, and too much space debris may result in large objects being unable to allocate enough contiguous memory, thus having to trigger GC or even Stop The World in advance.
two。 Replication algorithm (Copying)
In order to solve the problem of efficiency, the "replication" collection algorithm appeared. It divides available memory into two equal chunks according to capacity, using only one of them at a time. When this piece of memory is used up, copy the surviving objects to another piece, and then clean up the used memory space at once. In this way, one of the blocks is reclaimed every time, and the complex situations such as memory fragments do not have to be taken into account when allocating memory. As long as the pointer at the top of the stack is moved and the memory is allocated sequentially, the implementation is simple and efficient. It has two main disadvantages:
Efficiency problem: when the object survival rate is high, the number of replication operations is more, and the efficiency is reduced.
Space problem: the memory has been reduced by half; the extra space of the old era needs to be used as an allocation guarantee.
From Survivor, To Survivor uses the replication algorithm. This algorithm was not used in the old days.
3. Marking-finishing algorithm
The replication collection algorithm will perform more replication operations when the object survival rate is high, and the efficiency will become lower. More crucially, if you do not want to waste 50% of the space, you need additional space for allocation guarantees to deal with the extreme situation in which all objects in the used memory are 100% alive, so this algorithm generally could not be used directly in the old days.
According to the characteristics of the old years, someone proposed another "mark-organize" (Mark-Compact) algorithm, the marking process is still the same as the "mark-clear" algorithm, but the next step is not to directly clean up the recyclable objects, but to make all the surviving objects move to one end, and then directly clean up the memory beyond the end boundary. Also known as the tag-organize-clear algorithm.
4. Generation collection algorithm (Generational Collection)
The basic assumption of GC generation: the life cycle of most objects is very short, and the survival time is short. The "generation collection" algorithm divides the Java heap into the new generation and the old age, so that the most appropriate collection algorithm can be adopted according to the characteristics of each age. In the new generation, each garbage collection found that a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small amount of replication cost of surviving objects to complete the collection. In the old days, because the object had a high survival rate and no extra space to allocate guarantee, it was necessary to use the "mark-clean" or "mark-organize" algorithm for recycling.
After reading the above, do you have any further understanding of the example analysis of Java virtual machine garbage collection? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.