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

How to master JVM memory tuning

2025-01-21 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 "how to master JVM memory tuning". In the operation of actual cases, many people will encounter such a 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!

1.Full GC

The entire heap is sorted out, including Young, Tenured, and Perm. Full GC is slow because it needs to recycle the entire heap, so the number of Full GC should be reduced as much as possible.

two。 The cause of Full GC is 1) the Tenured is full.

When tuning, try to allow objects to be recycled in the new generation of GC, let objects survive in the new generation for a longer period of time, and do not create large objects and arrays to avoid creating objects directly in the old generation.

2) insufficient space for persistent Pemanet Generation

Increase the Perm Gen space, avoid too many static objects, and control the ratio of the new generation to the old generation.

3) System.gc () is displayed and called

Garbage collection should not be triggered manually. Try to rely on the mechanism of JVM itself.

In the process of tuning JVM, a large part of the work is to adjust FullGC. The following detailed description of the corresponding JVM tuning methods and steps.

JVM performance tuning method and step 1. Monitor the status of GC

Use various JVM tools to view the current log, analyze the current JVM parameter settings, and analyze the current heap memory snapshot and gc log, according to the actual regional memory partition and GC execution time, think whether to optimize.

To give an example: some phenomena before the system crash:

The time of each garbage collection is longer and longer, from the previous 10ms to about 50ms, and the FullGC time is also extended from 0.5s to 4,5s.

The frequency of FullGC is increasing, and the most frequent FullGC is carried out at intervals of less than 1 minute.

The memory of the older generation is getting larger and larger, and no memory is released every time the FullGC is followed by the older generation.

After that, the system will not be able to respond to new requests and gradually reach the critical value of OutOfMemoryError, at which point it is necessary to analyze the JVM memory snapshot dump.

two。 Generate dump files for the heap

The current Heap information is generated through JMX's MBean, which is a 3G (the size of the entire heap) hprof file. If you do not start JMX, you can generate this file through Java's jmap command.

3. Analyze dump files

To open this 3G heap information file, it is obvious that ordinary Window systems do not have such a large memory, so you must use high-configuration Linux and several tools to open the file:

Visual VM

IBM HeapAnalyzer

Hprof tools that come with JDK

Mat (Eclipse's specialized static memory analysis tool) is recommended.

Note: the file is too large, it is recommended to use Eclipse's special static memory analysis tool Mat to open the analysis.

4. Analyze the results to determine whether optimization is needed

If the parameters are set reasonably, there is no timeout log in the system, the frequency of GC is not high, and the time consuming of GC is not high, then it is not necessary to optimize GC. If the GC time exceeds 1-3 seconds, or frequent GC, it must be optimized.

Note: GC is generally not required if the following indicators are met:

Minor GC execution time is less than 50ms

Minor GC is executed infrequently, about once in 10 seconds.

Full GC execution time is less than 1s.

The frequency of Full GC execution is not frequent, not less than 10 minutes.

5. Adjust GC type and memory allocation

If the memory allocation is too large or too small, or if the GC collector is slow, you should first adjust these parameters, and first find one or more machines for beta, and then compare the performance of optimized machines with those without optimization, and make the final choice.

6. Constant analysis and adjustment

Through constant trial and error, analyze and find the most appropriate parameters, if found the most appropriate parameters, then apply these parameters to all servers.

Cms parameter optimization step process

Let me move on to the key parameter configuration of JVM (for reference only).

JVM tuning parameters refer to 1. Settings for JVM heap

Generally, the minimum and maximum values can be defined by-Xms-Xmx. In order to prevent the garbage collector from shrinking the heap between the minimum and maximum, the maximum and minimum are usually set to the same value.

two。 The young and the old

Heap memory is allocated according to the default ratio (1:2), which can be resized either by adjusting the ratio between the two (NewRadio) or for the recycling generation.

For example, the younger generation uses-XX:newSize-XX:MaxNewSize to set its absolute size. Similarly, to prevent the heap of the younger generation from shrinking, we usually set the-XX:newSize-XX:MaxNewSize to the same size.

3. The size of the younger generation and the older generation is reasonable.

1) the younger generation will inevitably lead to the younger generation, the older generation will prolong the cycle of the normal GC, but will increase the time of each GC; the younger generation will lead to more frequent Full GC.

2) the younger generation will inevitably lead to the older generation, the younger generation will cause the ordinary GC to be very frequent, but each time the GC time will be shorter; the older generation will reduce the frequency of Full GC.

How to choose should depend on the distribution of the life cycle of application objects: if there are a large number of temporary objects in the application, you should choose a larger younger generation; if there are relatively more persistent objects, the older generation should increase appropriately. But many applications do not have such obvious features.

The choice should be based on the following two points:

(1) in line with the principle of minimizing Full GC, let the older generation cache commonly used objects as much as possible, and the default proportion of JVM at 1:2 is the same reason.

(2) by observing the application for a period of time, to see how much memory the older generation will occupy at the peak, and increase the younger generation according to the actual situation without affecting Full GC, for example, the proportion can be controlled at 1:1. But the older generation should be given at least 1 prime 3 room for growth.

4. On well-configured machines (such as multi-core, large memory), you can choose a parallel collection algorithm for the older generation:-XX:+UseParallelOldGC.

5. Thread stack setting: each thread opens a 1m stack by default, which is used to store stack frames, call parameters, local variables, and so on. For most applications, this default value is too much. 256k is generally sufficient.

In theory, reducing the stack of each thread can result in more threads with the same memory, but this is actually limited by the operating system.

This is the end of "how to master JVM memory tuning". 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: 261

*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