For Java development students, JVM performance optimization can be said to be more difficult to grasp the knowledge. This is not only because JVM performance tuning requires arcane knowledge of the JVM, but also because it is difficult to use scenarios. As a result, JVM performance tuning is unfamiliar to many people and feels like a castle in the air. I did a JVM performance optimization in my work these days, which I think will be of great help to beginners of JVM tuning.

background

We all know that JVMS are divided into the new generation and the old generation, and we all configure parameters when starting an application to adjust the memory size for the JVM on which the application is running. But as we all know, a lot of times we just take a rough estimate, fill it in, and then we go up.

This is also the case at the author’s company, where the JVM memory size is generally set to be quite large. After all, it is better to have large memory than to run out of memory, resulting in a lot of wasted memory. So the task the author was given was to go through all the applications, adjust the appropriate memory parameters, and optimize the performance of the JVM.

Tuning of actual combat

To tune your application for JVM performance, you first need to know how it is running. It’s like going to the doctor at the hospital and having the doctor look at you, smell you, and ask you questions before you prescribe medicine. In Java, there are many ways to look inside the JVM, such as the various command work provided by the JDK. The author’s company uses Prometheus for monitoring, so we can see the JVM performance of the application directly from Prometheus.

JVM Misc, JVM Memory Pools (Heap), JVM Memory Pools (non-heap), and Garbage Collection are the main components of Prometheus. JVM Memory Pools (Heap) and Garbage Collection

JVM Memory Pools (Heap) display the usage of JVM Heap Memory, including Survivor areas of the new generation, Eden Space, and old age.

Garbage Collection shows how often Garbage is collected by the JVM, including the frequency of Garbage Collection (OPS is a number of Garbage collections per second, usually 0.5 is a reasonable value), the duration of each GC pause (generally less than 80ms is a reasonable value), and the amount of memory allocated to the new generation/advanced generation.

One of the easiest ways to optimize JVM performance is to look at the frequency of Garbage Collection GC and pause times, and we can roughly determine the memory utilization efficiency of an application. Then, based on the actual situation of these two values, adjust them to a reasonable range to improve JVM utilization.

If the GC frequency of an application is only 0.02, i.e. 0.02 GC per second, then it takes 50 seconds to GC, then the GC frequency is very low. At this time, it is likely that large Cenozoic space is allocated, which makes it necessary to GC once a long time. At this point, we look at the pause time, and if the pause time is also short, we can determine that the application has room to optimize its memory.

In this case, it is generally to reduce the allocation of new generation space. Once the space of the new generation becomes smaller, the time allotted will be reduced. Once the space is allocated, the GC operation is initiated. We simply adjust the MEMORY space of the JVM to increase the frequency of GC so that it is in a reasonable space.

In memory space adjustment, in order to avoid problems caused by violent fluctuations in memory, we generally run a little bit of small adjustment. Just tweak it a little bit and try it out, it’s fine and then adjust it to the target value. After all, this is a production environment, and if you have a fork, you have to take the bucket and run. Better to be cautious!

And now you know how to do it. The next step is simply to adjust the three parameters of the JVM memory space (-xmx-Xms-XMN) to get GC frequency and GC pause times in a reasonable range.

Application level optimization

In addition to GC frequency and GC pause times, we can also analyze JVM memory consumption based on the type of application.

For example, for an interface-type system, many requests end in less than a second. For these types of requests, memory is allocated when they enter the application, and is immediately reclaimed when they finish, leaving very few objects. The JVM memory profile for this application looks something like this: the younger generation consumes a lot of memory and recycles it periodically, but the older generation consumes less. For applications that are persistent, such as applications that are long in duration. Because it lives longer, more objects are likely to be promoted to the old age, so the memory consumption of the old age is higher.

By looking at memory consumption in the younger and older GENERATIONS of the JVM, combined with the characteristics of the application itself, we can find out what is wrong with the application and optimize it accordingly. For example, an application somewhere stores a large amount of temporary data into the content every time, which makes it possible for the JVM to burst GC, causing the application to stall.

conclusion

To summarize the tuning approach for this article: Tune the JVM memory space to the best possible state by observing GC frequency and pause times. Remember to take small steps during the adjustment process to avoid violent memory fluctuations affecting online services. This is actually the simplest way to tune JVM performance, and it can be considered crude tuning. However, there are more and more detailed parameters for JVM performance tuning, which we’ll talk about at a later opportunity.

In addition, looking at the younger and older GENERATIONS of JVMS can help you tailor your applications to improve their performance.

If you are not familiar with the fundamentals of the JVM, you may not be able to follow this article. I recommend that you check out my “Getting Started on the JVM” series, which will give you a sense of what the JVM is all about. After reading this article, you must have a feeling of enlightenment!

Portal: Introduction to JVM Basics – shuyi Chen’s blog

That’s it for JVM performance tuning.

Thank you for reading. If this article is helpful to you, click “Watching”, or share it on moments.