About tomcat memory overflow and expansion under linux

For problems such as tomcat memory overflow -----

In the production environment, if the Tomcat memory setting is not good, memory overflow will easily occur. It is not the same to cause memory overflow, and the processing method is also different.

1. The reasons for the tomcat memory overflow are:

OutOfMemoryError: Java heap space
OutOfMemoryError: PermGen space
OutOfMemoryError: unable to create new native thread.

Tomcat memory overflow solution

For the first two cases, if the application itself has no memory leaks, it can be solved by setting the tomcat jvm parameters. (-Xms -Xmx -XX:PermSize -XX:MaxPermSize)

The last one may need to adjust the operating system and tomcat jvm parameters at the same time to achieve the goal (see https://blog.csdn.net/oufua/article/details/70153742).

The setting of the JVM heap refers to the setting of the memory space that the JVM can allocate and use during the running of the java program.

  JVM It is automatically set at startup Heap size value, its initial space(which is-Xms)is 1 of physical memory/64,maximum space(-Xmx)is 1 of physical memory/4. 

  usable JVM which provided-Xmn -       Xms -Xmx and other options can be set. Heap size the size of Young Generation and Tenured Generaion Sum.

  exist JVM Medium if 98%time is used for GC and available Heap size less than 2%This exception message will be thrown.

  Heap Size The maximum should not exceed 80 of the available physical memory%,generally will-Xms and-Xmx option is set to the same, while-Xmn is 1/4 of-Xmx value.

2. Modify the jvm parameters of tomcat

Configure tomcat variables ---- in setenv.sh in the same directory as catalina.
(in the bin directory of tomcat)
Modify the setenv.sh file (not by default, you need to create a new setenv.sh), write (the size is modified according to your own situation):

export CATALINA_OPTS="$CATALINA_OPTS -Xms4096m"
export CATALINA_OPTS="$CATALINA_OPTS -Xmx4096m"
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=2048m"

After the setting is successful, we can check the current tomcat memory by command ps -ef |grep java

root       536     1 28 20:43 pts/0    00:00:19 /usr/local/jdk1.7.0_80/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-8.5.42/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Xms4096m -Xmx4096m -XX:MaxPermSize=2048m -Dignore.endorsed.dirs= -classpath /usr/local/apache-tomcat-8.5.42/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.42/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-8.5.42 -Dcatalina.home=/usr/local/apache-tomcat-8.5.42 -Djava.io.tmpdir=/usr/local/apache-tomcat-8.5.42/temp org.apache.catalina.startup.Bootstrap start
root       569 32276  0 20:44 pts/0    00:00:00 grep java

Up posture:

Common parameter types (configuration memory)

Configure the heap area: -Xms, -Xmx, -XX:newSize, -XX:MaxnewSize, -Xmn
Configure non-heap area: -XX:PermSize, -XX:MaxPermSize

back to the top
Heap parameter configuration

1. -Xms : Indicates the initial memory allocation size of the java virtual machine heap memory, which is usually 1/64 of the available memory of the operating system, but it still needs to be allocated according to the actual situation. It is possible that when such a rule is actually allocated, the designed software hangs before it can run.
2. -Xmx: Indicates the maximum upper limit of the java virtual machine heap memory that can be allocated, usually 1/4 of the available memory of the operating system. However, during the development process, the two parameters -Xms and -Xmx are usually configured with the same value. The purpose is to waste resources without re-partitioning the size of the heap area after the java garbage collection mechanism cleans up the heap area.
Generally speaking, for memory allocation in the heap area, you only need to configure the above two parameters reasonably.
back to the top
Non-heap parameter configuration

1. -XX:PermSize: Indicates the initial memory allocation size of the non-heap area, which is abbreviated as permanent size (persistent memory)
2. -XX:MaxPermSize: Indicates the maximum upper limit of the memory allocated to the non-heap area

Posted by Ace_Online on Sun, 22 Jan 2023 22:47:40 +0530