Spring boot project tuning

Posted by blue-genie on Fri, 17 Dec 2021 03:59:15 +0100

Project tuning overview

As a senior engineer, project tuning must be mastered.
In SpringBoot projects, tuning is done mainly through configuration files and configuration of JVM parameters.

1, Modify profile

About modifying the configuration file application properties.
Detailed configuration file modification document of SpringBoot project

The more important ones are:

server.tomcat.max-connections=0 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-header-size=0 # Maximum size, in bytes, of the HTTP message header.
server.tomcat.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content.
server.tomcat.max-threads=0 # Maximum number of worker threads.
server.tomcat.min-spare-threads=0 # Minimum number of worker threads.

2, Jvm tuning

There is a guidance note on Jvm tuning on the Oracle official website:

Instructions on Jvm tuning on Oracle official website

https://docs.oracle.com/middleware/11119/wls/PERFM/jvm_tuning.htm#i1146060

If you are interested, you can go and have a look.

3, Jvm tuning practice

1. Case where JVM parameters are not set#

I now have a project. By default, no Jvm parameters are set.
Let me start it.

Look at stack allocation:
Obviously, the default maximum heap memory allocation is 8 G. Obviously unreasonable.

2. Let's set the Jvm parameters#

For example, to configure a JVM, a large set of parameters:

-XX:MetaspaceSize=128m 
-XX:MaxMetaspaceSize=128m 
-Xms1024m 
-Xmx1024m 
-Xmn256m 
-Xss256k 
-XX:SurvivorRatio=8
-XX:+UseConcMarkSweepGC

Mode 1:
If you use development tools such as IDEA to start and run the project, it is too convenient to debug JDK.
You only need to set the parameter value to VM options.

 

After setting successfully, my GC log and stack allocation are OK.

GC log:

 

Stack allocation:

Mode 2:
It is applicable to the setting when the project is deployed and started by using script or command line.

First, package the project under the project path:
Clean up the project

mvn clean

 

Package new project:

mvn package -Dmaven.test.skip=true

After packaging, enter the path where the Jar package can be run:

 

Perform the operation of starting and setting Jvm parameters.

$ java -jar 
-XX:MetaspaceSize=128m 
-XX:MaxMetaspaceSize=128m 
-Xms1024m 
-Xmx1024m 
-Xmn256m 
-Xss256k 
-XX:SurvivorRatio=8 
-XX:+UseConcMarkSweepGC 
newframe-1.0.0.jar

At this time, if you look at the monitoring again, you will find that it is already Ok.
The stack is started according to the Jvm parameters set at startup.

 

For the meaning of JVM parameters of these settings, please refer to the tuning document officially given by oracle in step 2.

Let me briefly say here:

-XX:MetaspaceSize=128m (Meta space (default size)  
-XX:MaxMetaspaceSize=128m (Meta space (maximum size)  
-Xms1024m (Heap (maximum size)  
-Xmx1024m (Heap (default size)  
-Xmn256m (Cenozoic size)  
-Xss256k (Maximum stack depth (size)  
-XX:SurvivorRatio=8 (Cenozoic zoning proportion 8:2)  
-XX:+UseConcMarkSweepGC (Specify the garbage collector to use, which is used here CMS Collector)  
-XX:+PrintGCDetails (Print detailed GC (log)

Knowledge points:

After JDK8, - XX:PermSize and - XX:MaxPermGen are removed and replaced by

-XX:MetaspaceSize=128m (Meta space (default size)  
-XX:MaxMetaspaceSize=128m (Meta space (maximum size)  

JDK 8 starts to put the class metadata into the localized heap memory. This area is called Metaspace, which is called Metaspace in Chinese.
What are the benefits of using localized memory? The most direct expression is Java Lang. outofmemoryerror: the permgen space problem will no longer exist, because the default class metadata allocation is only limited by the local memory size, that is, how much local memory is left, Theoretically, the size of the Metaspace can be as large as possible (it seems that the capacity is also related to the virtual memory of the operating system? It is not clear here), which solves the problem of insufficient space. However, it is obviously unrealistic to make the Metaspace infinite, so we should also limit the size of the Metaspace:

Use the - XX:MaxMetaspaceSize parameter to specify the size of the Metaspace area.

By default, the JVM dynamically sets the size of MaxMetaspaceSize as needed at runtime.

 

Topics: Java Spring Boot