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

Java memory allocation and recovery strategy

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

Share

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

This article introduces the relevant knowledge of "Java memory allocation and recovery strategy". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Memory allocation and recovery policies Minor GC and Full GC

Minor GC: occurs in the new generation, because the survival time of the new generation object is very short, so the Minor GC will be executed frequently, and the execution speed will generally be faster.

Full GC: also known as Major GC, occurs in the old age, when objects live for a long time, so Full GC is rarely executed, and the execution speed is much slower than Minor GC.

1. Objects are assigned first in Eden

In most cases, objects are allocated in the Cenozoic Eden zone, and when there is not enough space in the Eden zone, the Minor GC is initiated.

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

Large objects are objects that require contiguous memory space. The most typical large objects are very long strings and arrays.

It is common for large objects to trigger garbage collection in advance to get enough contiguous space to allocate to large objects.

-XX:PretenureSizeThreshold. Objects larger than this value are allocated directly in the old age to avoid a large amount of memory replication between the Eden area and the Survivor area.

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

Define an age counter for an object, which is born in Eden and still alive through Minor GC, will be moved to Survivor, the age will increase by 1 year, and when it reaches a certain age, it will move into the old age.

-XX:MaxTenuringThreshold is used to define the threshold of age.

4. Dynamic object age determination

The virtual machine does not always require that the age of the object must reach MaxTenuringThreshold in order to promote the old age. If the total size of all objects of the same age in Survivor is more than half of the Survivor space, then the object whose age is greater than or equal to that age can directly enter the old age without waiting for the required age in MaxTenuringThreshold.

5. Space allocation guarantee

Before the occurrence of Minor GC, the virtual machine first checks whether the maximum contiguous space available in the old era is greater than the total space of all objects in the new generation, and if the condition is true, then Minor GC can confirm that it is secure.

If not, the virtual machine checks to see if the HandlePromotionFailure setting allows for guarantee failure, and if so, it will continue to check whether the maximum available contiguous space in the old age is greater than the average size of the object promoted to the old age. If it is larger, it will try to have a Minor GC; if it is less than, or if the HandlePromotionFailure setting does not allow risk, then a Full GC will be carried out.

Trigger conditions of Full GC

For Minor GC, the trigger condition is very simple, when the Eden space is full, a Minor GC will be triggered. On the other hand, Full GC is relatively complex with the following conditions:

Corresponding brain map

1. Call System.gc ()

It is only recommended that the virtual machine perform Full GC, but the virtual machine does not necessarily do so. This approach is not recommended, but instead allows the virtual machine to manage memory.

two。 Lack of space in the old days

The common scene of lack of space in the old age is that the large objects mentioned earlier directly enter the old age, and the long-term living objects enter the old age, and so on.

In order to avoid the Full GC caused by the above reasons, you should try not to create large objects and arrays. In addition, you can adjust the size of the new generation through the-Xmn virtual machine parameters, so that the object can be recycled in the new generation as far as possible, so that it will not enter the old age. You can also adjust the age of the object to the old age through-XX:MaxTenuringThreshold, so that the object can survive in the new generation for a longer period of time.

3. Space allocation guarantee failed

Minor GC that uses the replication algorithm requires old memory space as a guarantee, and a Full GC will be performed if the guarantee fails. Please refer to the fifth section above for details.

4. JDK 1.7 and previous permanent generation space is insufficient.

Before JDK 1.7, the method area of HotSpot virtual machine was implemented with permanent generation, which stored some Class information, constants, static variables and other data.

When there are a large number of classes to load, reflected classes, and methods called in the system, the permanent generation may be full and Full GC will be executed if it is not configured to adopt CMS GC. If it still fails to recycle after Full GC, the virtual machine throws a java.lang.OutOfMemoryError.

In order to avoid the Full GC caused by the above reasons, the method can be adopted to increase the permanent space or switch to the use of CMS GC.

5. Concurrent Mode Failure

During the execution of CMS GC, there are also objects to be put into the old age, and when the old age is out of space (perhaps due to the temporary shortage of space caused by too much floating garbage in the GC process), a Concurrent Mode Failure error will be reported and Full GC will be triggered.

This is the end of "Java memory allocation and recovery Strategy". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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