A brief discussion on the correct posture of Tomcat memory configuration

A brief discussion on the correct posture of Tomcat memory configuration

1. Background

Although I have read many blogs or articles, I have not found any comprehensive articles on JVM memory allocation methods. Many of them are just copy and paste.

For the sake of rigor, this article specifically notes that it only introduces the memory allocation based on the HotSpot VM virtual machine and JDK1.7. The discussion about GC is also based on CMS concurrent collection (not G1) to prevent experts from criticizing it.

The current mainstream JVM is HotSpot VM (followed by J9 VM and Zing VM), and most blog posts are based on versions before JDK 1.7.

(Note: Because different virtual machine implementations and different JDKs have different memory distributions, the memory structures mentioned in the following article are only logical structures, not the physical structures of memory)

This article only introduces the method of memory allocation. The specific garbage collection mechanism and the principle of memory structure are not the focus of this article. I also hope that through this article, everyone can have a little understanding of JVM. The editor does not have a thorough understanding of JVM and does not want to mislead others.

2. Overall memory structure

If you just want to solve the problem and don't want to understand the reasons, please skip this section

This article introduces the structure of the garbage collected memory area (referred to as the GC heap, excluding the program counter, stack, and local method stack), quoting a great saying "The Life of a Java Object"

I am an ordinary Java object. I was born in the Eden area. In the Eden area, I also saw little brothers (other Java objects) who looked very similar to me. We played in the Eden area for quite a long time. One day, there were too many people in the Eden area (which would trigger Young GC, and each GC would add one year to the age), so I was forced to go to the "From" area of ​​the Survivor area. Since I went to the Survivor area, I began to drift, sometimes in the "From" area of ​​the Survivor, sometimes in the "To" area of ​​the Survivor, with no fixed place to live (each Young GC required the from area and to area in the Survivor area to be "swapped"). It wasn’t until I was 18 years old (had 18 Young GCs) that my father told me that I was an adult and it was time for me to go out into the society. So I went to the older generation. There were a lot of people there, and they were all quite old. I also met a lot of people here. In the old generation, I lived for 20 years and then was recycled (Old GC).

To explain, first of all, the overall memory is divided into young generation (young), old generation (old), and permanent generation (permanent), as shown below

Young generation: (garbage collection for the young generation is referred to as Young GC)

The young generation is divided into eden area and survivor area

1. Eden area is where new Object() is born

2. The survivor area is the storage area for objects that survive garbage collection. The survivor area is divided into the from area and the to area.

2.1.from area: After GC recovery, objects that are still alive in the eden area and the to area will be stored in the from area

2.2.to area: After GC recovery, the objects that are still alive in the eden area and the from area will be transferred to the to area

2.3. Because of the operations in 2.1 and 2.2, the surviving objects in the from area and the to area are transferred back and forth, and one area is always empty.

Old generation: (Garbage collection for the old generation is referred to as Old GC)

After 18 Young GCs, objects that are still alive in the young generation are transferred from the young generation to the old generation.

When the old generation is full, the Old GC will be triggered, and the surviving objects will continue to remain in the old generation until they are recycled after 20 Old GCs.

Permanent generation: (Full GC for the recovery of young generation + old generation + permanent generation)

It is an implementation of the Java method area of ​​HotSpot VM, which usually stores class information, constant pool, static variables, JIT-compiled code and other data (simply understood as the storage area for compiled code, that is, it can be understood as: when our Java project is running, the more class files are loaded, the more permanent generation memory space is required)

(Note: It is said that the permanent generation is a concept unique to the Hotspot virtual machine. Other JVMs do not have this thing. In Java 8, the permanent generation was completely removed and replaced by another local memory that is not connected to the heap - the metaspace)

3. Common memory problem explanation

Common Problem 1 java.lang.OutOfMemoryError: Java heap space ----JVM Heap (heap) overflow

Cause: During the project running phase, there are too many new objects, which fills up the configured maximum memory, resulting in this error.

Solution: Manually set the size of Xms and Xmx.

Common problem 2 java.lang.OutOfMemoryError: PermGen space ----PermGen space (permanent generation) overflow

Cause: This error occurs when there are many Java files in the developed project (i.e. the project is large and many files are loaded by the JVM)

Solution: Manually set the MaxPermSize size.

Common Problem 3 java.lang.StackOverflowError ---- Stack Overflow

Reason: Usually it is caused by too many levels of recursion in a code logic.

Solution: Modify the recursive code and control the number of recursive layers

4. Memory allocation method (suggested, not a cure-all)

This article only introduces some commonly used configuration parameters. Usually, the permanent generation does not count as heap memory (it occupies another piece of memory separately), and the new generation occupies 1/2 of the old generation, that is, 1/3 of the entire heap memory. According to this principle, we give a configuration example.

For example, the server can provide 1G of memory for project use. Based on the above figure, we give the following configuration.

Operation mode:

-server mode, better performance when multiple CPUs are used

New generation and old generation: (usually the new generation and old generation are not configured separately, so just configure the entire memory heap size)

-Xms384m Initial memory space of the memory heap

-Xmx768m Maximum memory space for the memory heap

Permanent generation: (new generation, the remaining memory of the old generation configuration is reserved for the permanent generation) --- note that jdk1.8 has been removed

-XX:PermSize=128m Permanent generation initialization size

-XX:MaxPermSize=256m Maximum memory space for permanent generation (default is 64m)

4. Tomcat memory configuration methods in different environments

We have explained various memory problems in detail and briefly introduced configuration parameters. Now let's introduce the specific configuration methods in various environments.

1. Start tomcat using the command line:

Modify TOMCAT_HOME/bin/catalina.sh (catalina.bat in Windows), add the following statement at the top of the file

JAVA_OPTS="-server -Xms384m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256m"

2. If tomcat is registered as a windows service, you can modify it using /bin/tomcat8w.exe in the tomcat directory. As shown below

3. If you are using myeclipse to develop and start tomcat, the above changes will not work. You can set it as follows:

In Myeclipse->preferences->myeclipse->servers->tomcat->tomcat×.×->Optional Java VM arguments in the JDK panel, add the following:

-server -Xms384m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256m

Finally, two words:

No matter what is configured and what the parameter values ​​are, you need to debug it continuously according to the actual project and don't give up easily.

For example, the memory configuration of Tomcat is not the bigger the better. The best configuration is the one that suits the project/server.

This is the end of this article about the correct way to configure Tomcat memory. For more information about Tomcat memory configuration, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to configure the maxPostSize value of built-in Tomcat in Spring Boot
  • Detailed steps for configuring springboot using external tomcat in idea
  • Three ways to configure JNDI data source in Tomcat
  • Solution for Tomcat to place configuration files externally
  • Detailed explanation of optimized configuration of Tomcat user management

<<:  Detailed explanation of important cascading concepts in CSS

>>:  Zen coding for editplus example code description

Recommend

How to modify the default storage engine in MySQL

mysql storage engine: The MySQL server adopts a m...

Get / delete method to pass array parameters in Vue

When the front-end and back-end interact, sometim...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

JavaScript to implement slider verification code

This article shares the specific code of JavaScri...

MySQL database connection exception summary (worth collecting)

I found a strange problem when deploying the proj...

Summary of how to add root permissions to users in Linux

1. Add a user . First, use the adduser command to...

Detailed explanation of three ways to import CSS files

There are three ways to introduce CSS: inline sty...

Usage and demonstration of ref in Vue

ref definition: used to register reference inform...

W3C Tutorial (5): W3C XML Activities

XML is designed to describe, store, transmit and ...

MySql index improves query speed common methods code examples

Use indexes to speed up queries 1. Introduction I...

Detailed explanation of common commands in Docker repository

Log in docker login Complete the registration and...

How MLSQL Stack makes stream debugging easier

Preface A classmate is investigating MLSQL Stack&...

Detailed explanation of chmod command usage in Linux

chmod Command Syntax This is the correct syntax w...