Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the memory allocation strategy in JVM?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how the memory allocation strategy in JVM is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Memory allocation strategy

One of the important things about understanding GC is to understand JVM's memory allocation strategy: where objects are allocated and when objects are reclaimed.

The automatic memory management advocated in Java technology system can be summed up in two parts: allocating memory to objects and reclaiming the memory allocated to objects.

As we all know, Java object allocation is done on the Java heap, although there are JIT compiled and split into scalar types and briefly allocated on the stack. If the generation algorithm is used, then the new objects are assigned to the Eden area of the new generation. If the local thread allocation buffer is started, it will be allocated on the TLAB by thread priority.

In fact, the allocation rules for Java are not 100% fixed, depending on which garbage collector combination is currently being used and the setting of memory-related parameters in the virtual machine.

To put it simply, object memory allocation is mainly allocated in the heap. However, the rules for allocation are not fixed, depending on the combination of collectors used and the setting of JVM memory-related parameters.

The following Serial and Serial Old collectors do a summary of memory allocation and recycling strategies.

1. Object priority allocation in Cenozoic Eden

First, let's take a look at the memory allocation of the new generation:

Memory allocation:

The JVM memory is divided into a large Eden space (80%) and two small Servivor (10% each). When recycling, the surviving objects in Eden and Survivor are copied directly to another Servivor space by replication algorithm, and finally the data in the hospital Eden space and the original Survivor space are cleaned up.

In most cases, objects are allocated in the Cenozoic Eden zone. When there is not enough space in the Eden area to allocate, JVM will initiate a Minor GC.

Two concepts are explained here:

New generation GC (Minor GC): refers to the garbage collection actions that occur in the new generation. Because most Java objects are characterized by immortality, Minor GC is very frequent and the speed is relatively fast.

Old GC (Major GC/Full GC): refers to the old GC, the emergence of Major GC, usually accompanied by a Minor GC, but unlike Minor GC, the speed of Major GC is more than ten times slower.

two。 The big object went straight into the old age.

Let's first define so-called large objects: large objects, which refer to Java objects that require a lot of contiguous memory space. The most typical large objects can be very long strings and arrays.

JVM's attitude towards large objects:

Large objects are very troublesome for the memory allocation of JVM. If we allocate large objects among the new generation, it is easy to trigger garbage collection in advance to get enough contiguous space to "place" them when there is still plenty of memory. 、

In order to avoid the waste of resources and time caused by unwanted GC activities, the allocation strategy can be adopted to allocate large objects directly to the old age, and the virtual machine also provides the-XX:PretenureSizeThreshold parameter, so that objects larger than this setting value can be allocated directly in the old age.

-XX:PretenureSizeThreshold is only valid for Serial and ParNew collectors.

3. Long-term surviving objects will enter the old age.

When JVM adopts the idea of generational collection to manage memory, in order to identify which objects should be placed in the new generation and which should be placed in the old age, JVM defines an object age counter for each object.

Object age counter: if an object is born in Eden and is still alive after the first Minor GC, and can be accommodated by Survivor, it can be moved to Survivor space, and the age counter will set the age of the object to 1. Each time an object passes a Minor GC in Survival, the age increases by 1 year, and when its age increases to a certain extent (through the parameter-XX:MaxTenuringThreshold setting) the default is 15, the object will enter the old age. To be the object of the old age.

4. Dynamic object age determination

In fact, some virtual machines do not always require that the age of the object must reach MaxTeruringThreshold in order to promote the old age. If the total size of all objects of the same age in the Survivor space is more than half of the Surivior space, the objects whose age is greater than or equal to that age can go straight to the old age without waiting for the required age in the MaxTeruringThreshold.

5. Space allocation guarantee

Before the occurrence of Minor GC, the virtual machine first checks whether the largest contiguous space available in the old era is larger than the total space of all objects in the new generation. If this condition is true, then Minor GC can ensure that it is safe. If not, the virtual machine checks to see if the HandlePromotionFaiure setting value allows the guarantee to fail. If allowed, it will continue to check whether the maximum contiguous space available in the old age is larger than the average size of previous promotions to the old age object, and if so, an Minor GC will be attempted, although this time the GC is risky; if it is less than, or the HandlePromotionFaiure setting does not allow risk, then a Full GC will be done instead.

The so-called adventure: that is, when the Survivor area used for rotation cannot afford the memory of the objects that survive in the new generation, it needs the old age to allocate the objects that cannot be accommodated by the Survivor directly into the old age, as long as it is in the old age.

These are all the contents of the article "what is the memory allocation strategy in JVM?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report