In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you Java virtual machine memory allocation and recycling strategy example analysis, I believe most people do not know how, so share this article for your reference, I hope you read this article after a lot of harvest, let us go to understand it!
Memory allocation and reclamation strategy
The most fundamental goal of automatic memory management in Java technology architecture is to solve two problems automatically: automatically allocating memory to objects and automatically reclaiming memory allocated to objects.
1. reviewed
Memory allocation for objects should, conceptually, be allocated on the heap (although it is actually possible to disassemble them into scalar types after just-in-time compilation and allocate them indirectly on the stack). In classical generational designs, newborn objects are usually assigned to the new generation, and in rare cases (e.g., object sizes above a certain threshold) may be assigned directly to the old generation. The rules for object allocation are not fixed, and the Java Virtual Machine Specification does not specify the details of creating and storing new objects, depending on which garbage collector the virtual machine is currently using and the settings of memory-related parameters in the virtual machine.
(1) Object priority assigned in Eden
In most cases, objects are distributed in the Cenozoic Eden zone. When the Eden zone does not have enough space to allocate, the virtual machine initiates a Minor GC.
1. The case where Eden has enough space
virtual machine parameter
-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
parameter description
Try to allocate three objects of 2MB size and one object of 4MB size. At runtime, the Java heap size is limited to 20MB by-Xms20M, -Xmx20M, -Xmn10M, of which 10MB is allocated to the new generation and the remaining 10MB is allocated to the old generation. - XX: Survivor-Ratio=8 determines that the spatial ratio of Eden region to a Survivor region in the Cenozoic is 8:1.
package com.xiao.test.Test;public class test { private static final int _1MB = 1024 * 1024; public static void main(String[] args) { byte[] byte1,byte2,byte3; byte1 = new byte[2 * _1MB]; byte2 = new byte[2 * _1MB]; byte3 = new byte[2 * _1MB]; }}
2. There's not enough room in Eden.
Virtual machine parameters are the same
package com.xiao.test.Test;public class test { private static final int _1MB = 1024 * 1024; public static void main(String[] args) { byte[] byte1,byte2,byte3,byte4; byte1 = new byte[2 * _1MB]; byte2 = new byte[2 * _1MB]; byte3 = new byte[2 * _1MB]; byte4 = new byte[3 * _1MB]; }}
Apparently Minor GC was done.
(2) Large objects directly enter the elderly generation
1. What is a big object?
Large objects are Java objects that require a lot of contiguous memory space, typically long strings or arrays with a large number of elements.
2. Reasons to Avoid Large Objects in Java Virtual Machine
When allocating space, it tends to trigger garbage collection in advance when there is still a lot of space in memory, in order to obtain enough continuous space to place them. When replicating objects, large objects imply high memory replication overhead.
3. The benefits of big objects going straight into old age
Avoid copying back and forth between Eden and two Survivor regions, resulting in a large number of memory copy operations (HotSpot virtual machine provides-XX: PretenureSizeThreshold parameter, specifying that objects larger than this setting are allocated directly in the old age).
(3) Long-term survivors will enter the elderly generation
1. How does a virtual machine determine if an object is long-lived?
Memory reclamation requires the ability to decide which surviving objects should be placed in the new generation and which surviving objects should be placed in the old. To do this, the virtual machine defines an object age counter for each object, which is stored in the object header.
2. The process of increasing age and promotion to old age
Objects are usually born in Eden, and if they survive the first Minor GC and can be accommodated by Survivor, they are moved to Survivor space and their age is set to 1 year. Each time an object survives a Minor GC in the Survivor zone, its age increases by 1 year, and when its age increases to a certain extent (the default is 15), it is promoted to the old age. The age threshold for object promotion to older age can be set by parameter-XX: MaxTenuringThreshold.
3. Why long-lived objects will enter old age
We all know that the new generation garbage collection algorithm is marker-copy algorithm, if the long-lived objects are still stored in the new generation, then it will bring about the problem of increased cost of replication. So we put objects older than a certain age threshold in order to relieve the pressure on the Cenozoic generation for garbage collection.
(4) Age determination of dynamic objects
In order to better adapt to the memory status of different programs, HotSpot virtual machines do not always require that the age of objects must reach-XX:MaxTenuringThreshold to advance to the old age. If the sum of the sizes of all objects of the same age in Survivor space is greater than half of Survivor space, objects older than or equal to this age can directly enter the old age without waiting for the age required in-XX:MaxTenuringThreshold.
(5) Space allocation guarantee
1. Content of space allocation guarantee
Before Minor GC occurs, the virtual machine must first check whether the largest available contiguous space in the old generation is larger than the total space of all objects in the new generation. If this condition holds, then this Minor GC can be guaranteed to be safe. If not, the virtual machine will first check whether the-XX: HandlePromotionFailure parameter is set to allow guarantee failure; if it is allowed, it will continue to check whether the maximum available continuous space in the old age is larger than the average size of objects promoted to the old age. If it is larger, it will try to perform a Minor GC, although this Minor GC is risky; if it is smaller than, or-XX: HandlePromotionFailure is set to not allow risk, then it will perform a Full GC instead.
2. "Risk" means risk?
As mentioned above, the new generation uses the replication collection algorithm, but for memory utilization, only one of the Survivor spaces is used as a rotation backup, so when a large number of objects still survive after Minor GC-the most extreme case is that all objects in the new generation survive after memory reclamation, and the old generation needs to be allocated for guarantee, and the objects that Survivor cannot accommodate are directly sent to the old age, which is similar to loan guarantee in life. The old era needs to make such a guarantee, provided that the old era itself still has the remaining space to accommodate these objects, but it is impossible to know how many objects will survive this reclamation until the actual memory reclamation is completed, so we can only take the average size of the object capacity promoted to the old era in each previous reclamation as the experience value, and compare it with the remaining space of the old era to decide whether to perform Full GC to make more space for the old era.
3. Do we need to open the warranty?
Taking the historical average to compare is still a solution to bet on probability, that is, if the number of objects after a Minor GC survives suddenly increases, much higher than the historical average, it will still lead to guarantee failure. If there is a guarantee failure, then you have to honestly re-launch a Full GC, so the pause time is very long. Although the guarantee fails in the largest circle, it is usually still possible to turn on the-XX: HandlePromotionFailure switch to avoid Full GC too frequently.
The above is "Java virtual machine memory allocation and recycling policy sample analysis" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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.
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.