CentOS 7 Linux installation Tomcat 8-JVM memory optimization, centosjvm

Source: Internet
Author: User
Tags xms jconsole

CentOS 7 Linux installation Tomcat 8-JVM memory optimization, centosjvm

CentOS 7 Linux installation Tomcat 8-JVM memory optimization (What should I do? zgbn)

Refer to: CentOS 7 install Tomcat 8 in Linux

Configure the default memory JVM memory allocation when Tomcat starts JVM

JAVA_OPTS = "-server-Xms2048m-Xmx2048m-XX: PermSize = 128 M-XX: MaxPermSize = 128 m"

JVM memory configuration parameters Description
-Xmx The maximum value of Java Heap. The default value is 1/4 of the physical memory;
-Xms Java Heap initial value. It is best to set-Xms and-Xmx to the same value on the Server JVM. The default value can be retained on the JVM of the development and testing machine;
-Xmn The size of the Java Heap Young area. If you are not familiar with it, keep the default value;
-XX: PermSize Set the permanent storage area of the memory;
-XX: MaxPermSize Sets the permanent storage area of the maximum memory;
-XX: NewSize Sets the default size of the new generation of JVM heap;
-XX: MaxNewSize Sets the maximum size of the new generation of JVM heap;
-Xss The Stack size of each thread. It is best to keep the default value if you are not familiar with it;
JVM heap memory allocation

-Xms indicates that the JVM is initiated to allocate physical memory. The default value is 1/64 of the physical memory;
-Xmx specifies the maximum physical memory allocated during JVM runtime. The default value is 1/4 of the physical memory;

When the JVM is running, when the JVM idle heap memory is greater than 70% of the memory occupied by the JVM, the JVM will automatically release the minimum value for the memory that has been reached-Xms, when JVM adjusts its heap memory, it usually occurs after GC.


Memory Optimization
-Therefore, We can manually set-Xms to be equal to-Xmx to avoid adjusting the heap size after each GC.
-It is recommended to set the maximum heap memory value-Xmx is 80% of the maximum available memory value.


Memory Overflow
--Xms <-Xmx: JVM startup fails;
--Xmx> JVM startup fails when physical memory is available;
-Memory overflow (OutOfMemoryError: Java heap space) occurs when the memory used by the application to start and run exceeds-Xmx );

JVM non-heap memory allocation

-XX: The PermSize parameter specifies the initial non-heap memory value. The default value is 1/64 of the physical memory;
-XX: MaxPermSize sets the maximum non-heap memory size. The default value is 1/4 of the physical memory;

JVM non-heap memory (permanent memory zone) is used to store Class and Meta information. All classes of the application are placed in this zone when loaded by JVM. Unlike the Heap of an Instance, Garbage Collection does not clean up non-Heap memory (PermGen space) during the main program running.

Memory Optimization
-We recommend that you allocate the resources based on the actual situation of the application deployed on the server. You can use the jconsole tool to check the memory usage after JVM startup and then allocate the resources;
-We recommend that you set the-XX: PermSize and-XX: MaxPermSize values to be equal.


Memory Overflow
-When the JVM starts to load the application, if the memory used to load the class exceeds-XX: MaxPermSize, a non-heap memory overflow error (OutOfMemoryError: PermGen space) occurs );
-When the memory configured in-Xmx +-XX: MaxPermSize exceeds the physical memory available in the system, JVM startup fails;

JVM memory Exception error

Thanks for reference: http://blog.csdn.net/thunder0709/article/details/16855195

OutOfMemoryError: Java heap space

Memory overflow occurs in this case. This exception is thrown when 98% of the time in JVM is used for GC and the available Heap size is less than 2%.

OutOfMemoryError: PermGen space

This error is common when the web server pre-compile the JSP. If your web app uses a large number of third-party jar files, which exceeds the default jvm size, this error message is generated. If the web app uses a large number of third-party jar files or the application has too many class files, but the MaxPermSize setting is small, exceeding this will also cause excessive memory usage and overflow, or during hot deployment of tomcat, the Environment loaded above will not be cleaned up, but the context will be changed to a new deployment, so there will be more and more non-heap content.

OutOfMemoryError: unable to create new native thread

This phenomenon is rare and strange, mainly due to the ratio of jvm to system memory. This is strange because the JVM has been allocated a large amount of memory (such as 1.5 GB) by the system, and it must occupy at least half of the available memory.

View JVM memory information use jconsole tool to view JVM runtime memory

In the Windows system, JDK package is installed with the built-in jconsole tool. You can use the jconsole tool to link the remote server's JVM instance to view the JVM runtime memory.
By the way, we will introduce another JDK tool jvisualvm, which can be used to test the performance of JAVA programs and analyze program performance data ).
By the way, we will introduce another JDK built-in tool, jmc, which can be used to remotely monitor JVM. After running this tool, the main interface will introduce this tool, "What is Oracle Java Mission Control ?".

Location: % JAVA_HOME %/bin/jconsole.exe
Location: % JAVA_HOME %/bin/jvisualvm.exe
Location: % JAVA_HOME %/bin/jmc.exe

Use a JAVA program to view the JVM runtime memory
// Maximum available memory, corresponding to-Xmx Runtime. getRuntime (). maxMemory (); // The current JVM idle memory Runtime. getRuntime (). freeMemory (); // The total memory occupied by the current JVM. The value is equivalent to the memory used by the current JVM and the total Runtime of freemory. getRuntime (). totalMemory ();
Configure Tomcat to start with catalina. sh to configure JVM memory

To sum up, we have explained some precautions for JVM memory. The following describes how Tomcat starts to configure JVM memory allocation through catalina. sh.

Note: How to start Tomcat in catalina. sh mode [Click to view]

Go to the % TOMCAT_HOME %/bin directory and find the catalina. sh script.
1. test whether the startup service of catalina. sh is normal.
Run the following command to check the startup information of Tomcat.

[root@iZ28snxdn5mZ bin]# ./catalina.sh startUsing CATALINA_BASE:   /opt/apache/tomcat-8Using CATALINA_HOME:   /opt/apache/tomcat-8Using CATALINA_TMPDIR: /opt/apache/tomcat-8/tempUsing JRE_HOME:        /usrUsing CLASSPATH:       /opt/apache/tomcat-8/bin/bootstrap.jar:/opt/apache/tomcat-8/bin/tomcat-juli.jarTomcat started.[root@iZ28snxdn5mZ bin]# ./catalina.sh stop

2. Use the vim editor to open the catalina. sh script. Append the following code before executing the script.

JAVA_OPTS="-server -Xms896m -Xmx896m -XX:PermSize=128M -XX:MaxPermSize=128m"


Then run the script again to start the Tomcat service.

[root@iZ28snxdn5mZ bin]# ./catalina.sh startUsing CATALINA_BASE:   /opt/apache/tomcat-8Using CATALINA_HOME:   /opt/apache/tomcat-8Using CATALINA_TMPDIR: /opt/apache/tomcat-8/tempUsing JRE_HOME:        /usrUsing CLASSPATH:       /opt/apache/tomcat-8/bin/bootstrap.jar:/opt/apache/tomcat-8/bin/tomcat-juli.jarTomcat started.[root@iZ28snxdn5mZ bin]# ps -ef | grep tomcatroot     23795     1  3 20:06 pts/0    00:00:01 /usr/bin/java -Djava.util.logging.config.file=/opt/apache/tomcat-8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms896m -Xmx896m -XX:PermSize=128M -XX:MaxPermSize=128m -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dignore.endorsed.dirs= -classpath /opt/apache/tomcat-8/bin/bootstrap.jar:/opt/apache/tomcat-8/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache/tomcat-8 -Dcatalina.home=/opt/apache/tomcat-8 -Djava.io.tmpdir=/opt/apache/tomcat-8/temp org.apache.catalina.startup.Bootstrap startroot     23838 23179  0 20:06 pts/0    00:00:00 grep --color=auto tomcat[root@iZ28snxdn5mZ bin]# ./catalina.sh stopUsing CATALINA_BASE:   /opt/apache/tomcat-8Using CATALINA_HOME:   /opt/apache/tomcat-8Using CATALINA_TMPDIR: /opt/apache/tomcat-8/tempUsing JRE_HOME:        /usrUsing CLASSPATH:       /opt/apache/tomcat-8/bin/bootstrap.jar:/opt/apache/tomcat-8/bin/tomcat-juli.jarOpenJDK 64-Bit Server VM warning: ignoring option PermSize=128M; support was removed in 8.0OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0[root@iZ28snxdn5mZ bin]#

Note: in execution. /catalina. after sh stop, the log prompts us the following two sentences. The main meaning is that the two parameters-PermSize and-MaxPermSize are not required since Tomcat 8.0. So you can simply delete it in the configuration.
OpenJDK 64-Bit Server VM warning: ignoring option PermSize = 128 M; support was removed in 8.0
OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize = 128 m; support was removed in 8.0

Configure Tomcat and then start daemon. sh to configure JVM memory

To sum up, we have explained some precautions for JVM memory. The following describes how Tomcat starts to configure JVM memory allocation through catalina. sh.

Note: How to start Tomcat in catalina. sh mode [Click to view]

Go to the % TOMCAT_HOME %/bin directory and find the daemon. sh script.
1. test whether the daemon. sh startup service is normal.
Run the following command to check the startup information of Tomcat.

Note: we do not see the-server-Xms896m-Xmx896m memory allocation information from the process information, that is, Tomcat daemon. sh STARTUP script does not inherit catalina. sh script. sh: Configure JVM memory allocation parameters.

[root@iZ28snxdn5mZ bin]# ./daemon.sh start[root@iZ28snxdn5mZ bin]# ps -ef | grep tomcatroot     23891     1  0 20:14 ?        00:00:00 jsvc.exec -java-home /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/jre -user www -pidfile /opt/apache/tomcat-8/logs/catalina-daemon.pid -wait 10 -outfile /opt/apache/tomcat-8/logs/catalina-daemon.out -errfile &1 -classpath /opt/apache/tomcat-8/bin/bootstrap.jar:/opt/apache/tomcat-8/bin/commons-daemon.jar:/opt/apache/tomcat-8/bin/tomcat-juli.jar -Djava.util.logging.config.file=/opt/apache/tomcat-8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dignore.endorsed.dirs= -Dcatalina.base=/opt/apache/tomcat-8 -Dcatalina.home=/opt/apache/tomcat-8 -Djava.io.tmpdir=/opt/apache/tomcat-8/temp org.apache.catalina.startup.Bootstrapwww      23892 23891 19 20:14 ?        00:00:01 jsvc.exec -java-home /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/jre -user www -pidfile /opt/apache/tomcat-8/logs/catalina-daemon.pid -wait 10 -outfile /opt/apache/tomcat-8/logs/catalina-daemon.out -errfile &1 -classpath /opt/apache/tomcat-8/bin/bootstrap.jar:/opt/apache/tomcat-8/bin/commons-daemon.jar:/opt/apache/tomcat-8/bin/tomcat-juli.jar -Djava.util.logging.config.file=/opt/apache/tomcat-8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dignore.endorsed.dirs= -Dcatalina.base=/opt/apache/tomcat-8 -Dcatalina.home=/opt/apache/tomcat-8 -Djava.io.tmpdir=/opt/apache/tomcat-8/temp org.apache.catalina.startup.Bootstraproot     23934 23179  0 20:14 pts/0    00:00:00 grep --color=auto tomcat[root@iZ28snxdn5mZ bin]# 

2. Use the vim editor to open the daemon. sh script. Append the following code before executing the script.
JAVA_OPTS = "-server-Xms896m-Xmx896m"

[root@iZ28snxdn5mZ bin]# vim daemon.sh...

Search JAVA_OPTS = in vim. For example, configure JVM memory allocation parameters later.

Save and exit.
Stop the service and restart the service.

[root@iZ28snxdn5mZ bin]# ./daemon.sh stop[root@iZ28snxdn5mZ bin]# ./daemon.sh start[root@iZ28snxdn5mZ bin]# ps -ef | grep tomcatroot     23983     1  0 20:22 ?        00:00:00 jsvc.exec -java-home /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/jre -user www -pidfile /opt/apache/tomcat-8.5.27/logs/catalina-daemon.pid -wait 10 -outfile /opt/apache/tomcat-8.5.27/logs/catalina-daemon.out -errfile &1 -classpath /opt/apache/tomcat-8.5.27/bin/bootstrap.jar:/opt/apache/tomcat-8.5.27/bin/commons-daemon.jar:/opt/apache/tomcat-8.5.27/bin/tomcat-juli.jar -Djava.util.logging.config.file=/opt/apache/tomcat-8.5.27/conf/logging.properties -server -Xms896m -Xmx896m -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dignore.endorsed.dirs= -Dcatalina.base=/opt/apache/tomcat-8.5.27 -Dcatalina.home=/opt/apache/tomcat-8.5.27 -Djava.io.tmpdir=/opt/apache/tomcat-8.5.27/temp org.apache.catalina.startup.Bootstrapwww      23984 23983  7 20:22 ?        00:00:01 jsvc.exec -java-home /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/jre -user www -pidfile /opt/apache/tomcat-8.5.27/logs/catalina-daemon.pid -wait 10 -outfile /opt/apache/tomcat-8.5.27/logs/catalina-daemon.out -errfile &1 -classpath /opt/apache/tomcat-8.5.27/bin/bootstrap.jar:/opt/apache/tomcat-8.5.27/bin/commons-daemon.jar:/opt/apache/tomcat-8.5.27/bin/tomcat-juli.jar -Djava.util.logging.config.file=/opt/apache/tomcat-8.5.27/conf/logging.properties -server -Xms896m -Xmx896m -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dignore.endorsed.dirs= -Dcatalina.base=/opt/apache/tomcat-8.5.27 -Dcatalina.home=/opt/apache/tomcat-8.5.27 -Djava.io.tmpdir=/opt/apache/tomcat-8.5.27/temp org.apache.catalina.startup.Bootstraproot     24026 23179  0 20:22 pts/0    00:00:00 grep --color=auto tomcat[root@iZ28snxdn5mZ bin]#

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.