In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Java virtual machine memory allocation strategy is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Automatic memory management in Java technology system can be summed up as solving two problems automatically: allocating memory to objects and reclaiming memory allocated to objects.
I. Overview of memory allocation strategy
Memory sub-allocation of objects mainly refers to heap allocation (can also be allocated on the stack), objects are mainly allocated in the new generation Eden area, if the local thread allocation buffer is started, it is allocated on the TLAB according to thread priority. In a few cases, it will also be allocated directly in the old days, and the rules of allocation are not fixed, depending on the garbage collector combination and the setting of memory-related parameters in JVM.
Let's take the Serial/Serial Old collector as an example.
Second, memory allocation strategy 1. Object priority allocation in Eden area
In most cases, objects are allocated in the Eden zone of the new generation, and when there is not enough space in the Eden zone to allocate, the virtual machine will initiate a Minor GC.
two。 The big object went straight into the old age.
Large objects are Java objects that need a lot of continuous memory space. Typical large objects have long strings and arrays. Large objects are bad news for the memory allocation of virtual machines. When writing programs, we should avoid creating some short-lived large objects that die overnight. When large objects often occur, garbage collection will be triggered in advance to get enough continuous space to accommodate them when there is still a lot of memory space.
The virtual machine provides the-XX:PretenureSizeThreshold parameter to enable objects larger than this setting to be allocated directly in the old years, so as to avoid a large number of memory copies in Eden and two Survivor intervals. This parameter is only valid for Serial and ParNew collectors. If you have to use this parameter, you can consider the combination of ParNew and CMS.
The purpose of allocating large objects directly in the old days was to avoid a large amount of memory replication between the Eden area and the two Survivor regions.
3. Enter the old age according to the age of the object
The virtual machine defines an object age counter for each object. If the object is born in Eden and is still alive after the first Minor GC, and can be accommodated by Survivor, it will be moved to the Survivor space, and the age of the object will be set to 1. Each time the object survives Minor GC in the Survivor area, the age will increase by 1 year, and when its age increases to a certain extent (the default is 15), it will be promoted to the old age. You can set the age threshold through the-XX:MaxTenuringThreshold parameter.
4. Dynamic object age judgment
In order to better adapt to the memory situation of different programs, the virtual machine does not always require that the age of the object must reach the threshold in order to promote the old age; if the sum of all the objects of the same age in the Survivor area is more than half of the Survivor space, the objects whose age is greater than or equal to that age can directly enter the old age without waiting for the age required in-XX:MaxTenuringThreshold.
5. Space allocation guarantee
There are differences in the distribution of guarantee mechanisms before and after JDK6.
Before JDK6
When Minor GC occurs, JVM will first check whether the largest available contiguous space in the old era is greater than the sum of all objects in the new generation. If so, the Minor GC is safe this time. If not, JVM needs to determine whether HandlePromotionFailure allows space allocation guarantee.
If the guarantee is allowed to fail, JVM continues to check whether the largest available contiguous space in the old age is greater than the average size of the object promoted to the old age:
If it is greater than, then a YGC is performed normally, although it is risky (because the average size is judged, it is possible that the promotion target this time is much larger than the average)
If it is less than, or if the HandlePromotionFailure setting does not allow space allocation guarantee, then a FGC should be performed.
To sum up the above steps, we will first see whether the free space of the old era can accommodate all the objects of the new generation, and if not, see whether the allocation guarantee mechanism is enabled, and execute Minor GC first, otherwise Full GC will be carried out directly. In most cases, HandlePromotionFailure will still be enabled to allocate guarantees to avoid frequent Full GC.
After JDK6
HandlePromotionFailure no longer affects the space allocation guarantee strategy of virtual machines, and Minor GC will be carried out as long as the continuous space of the old era is larger than the total size of the new generation objects or the average size of previous promotions, otherwise Full GC will be carried out.
III. Conceptual charging
Minor GC and Major GC/Full GC:
The new generation of GC (Minor GC: abbreviation YGC) refers to the garbage collection action that occurs in the new generation. Because most Java objects have the characteristics of dying out, Monir GC is very frequent, and the general collection speed is relatively fast.
GC (Major GC/Full GC): refers to the GC that occurred in the old years, with the emergence of Major GC, which is often accompanied by at least one collection strategy of Minor GC (but not absolutely, in Parallel Scavenge), there is a direct Major GC strategy selection process. Major GC is generally more than 10 times slower than Major GC.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, 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.