tomcat memory configuration

1. Heap memory, first of all: java.lang.OutOfMemoryError: Java heap space Explanation: Heap size 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. The JVM will automatically Set the value of Heap size, the initial space (ie -Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) is 1/4 of the physical memory. You can use the options such as -Xmn -Xms -Xmx provided by the JVM to set it

. Tip : The maximum Heap Size should not exceed 80% of the available physical memory. Generally, the -Xms and -Xmx options should be set to the same, and -Xmn is 1/ -Xmx value of 4.

2. Non-heap memory, java.lang.OutOfMemoryError: PermGen space Reason: The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of ​​memory. This memory is mainly used by JVM to store Class and Meta information. Loader will be placed in the PermGen space, which is different from the Heap area where the class instance (Instance) is stored. GC (Garbage Collection) will not clean up the PermGen space during the main program runtime, so if your application has a lot of If CLASS is used, it is very likely that a PermGen space error will occur, which is common when the web server precompiles the JSP. If your WEB APP uses a large number of third-party jars, the size of which exceeds the default size of jvm (4M), then this error message will be generated.

3: If you use startup.bat, tomcat8.exe window to start,
modify E:\apachetomcatztgj\bin\catalina.bat
to add after the first line of the comment
rem ----------------------------------------
set JAVA_OPTS=-Xms512m -Xmx1024m -XX :PermSize=512M -XX:MaxNewSize=256m -XX:MaxPermSize=1024m
Just modify this place


4: If you use the service mode, only modify tomcat8w.exe, and in services.msc, change tomcat to automatic Just start it, no need to modify catalina.bat
java
java options: add
-XX:PermSize=512M
-XX:MaxPermSize=1024M

Initial memory pool: 1024M
Maximum memory pool: 1024M

Problems encountered in configuration:
1: Install under bin first Tomcat server, run cmd
services.bat install tomcat8



===== Idea tool jdk memory configuration in the project
-server -Xms256M -Xmx1024M -XX:PermSize=512m -XX:MaxPermSize=512m


===== ===========Extensions==============
Tomcat itself cannot run directly on the computer, and needs to rely on an operating system based on hardware and a Java virtual machine. The essence of Tomcat's memory overflow is JVM memory overflow, so at the beginning of this article, you should first introduce the knowledge of Java JVM in detail.

1. Java JVM Memory Introduction

The JVM manages two types of memory, heap and non-heap. According to the official statement: "The Java virtual machine has a heap, which is the runtime data area from which memory for all class instances and arrays is allocated. The heap is created when the Java virtual machine starts." "The heap in the JVM The external memory is called non-heap memory (Non-heap memory). In simple terms, the heap is the memory accessible to Java code, which is reserved for developers; non-heap is reserved for the JVM, so the memory required for method area, JVM internal processing or optimization (such as JIT compiled code) cache), each class structure (such as runtime constant pool, field and method data), and the code of methods and constructors are in non-heap memory, which is different from the heap, and its space is not released by the GC during runtime.

(1). Heap memory allocation
The initial allocated memory of the JVM is specified by -Xms, and the default is 1/64 of the physical memory; the maximum allocated memory of the JVM is specified by -Xmx, and the default is 1/4 of the physical memory. When the default free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of -Xmx; when the free heap memory is greater than 70%, the JVM will reduce the heap until the minimum limit of -Xms. Therefore, the server generally sets -Xms, -Xmx equal to avoid adjusting the size of the heap after each GC. You can use the options such as -Xmn -Xms -Xmx provided by the JVM to set the heap memory. Generally, the -Xms and -Xmx options should be set to the same, and -Xmn is 1/4 of the -Xmx value. The maximum value of the heap is recommended Set to 80% of the maximum available memory.

The size of the initialization heap is the size of the memory that the JVM requests from the system at startup. In general, this parameter is not important. However, some applications will rapidly occupy more memory under heavy load. At this time, this parameter is very important. If the memory used when the JVM is started is relatively small and many objects are initialized in this case , the JVM must repeatedly increase the memory to meet the usage. For this reason, we generally set -Xms and -Xmx to be the same size, and the maximum heap size is limited by the physical memory used by the system. Generally, applications that use large amounts of data use persistent objects, and memory usage is likely to grow rapidly. When the memory required by the application exceeds the maximum heap size, the JVM will prompt a memory overflow and cause the application service to crash. Therefore, if Xms exceeds the Xmx value, or the sum of the heap maximum and non-heap maximum exceeds the physical memory or the maximum limit of the operating system, the server will not start.

(2). Non-heap memory allocation
It is also called a permanently saved area, which is used to store Class and Meta information. Class is put into this area when it is loaded. It is different from the Heap area where the class instance (Instance) is stored, and the GC (Garbage Collection) will not clean up the PermGen space during the runtime of the main program. The JVM uses -XX:PermSize to set the initial value of non-heap memory, the default is 1/64 of the physical memory; the maximum non-heap memory size is set by XX:MaxPermSize, the default is 1/4 of the physical memory. The GC will not clean up the PermGen space, so if your app loads a lot of CLASS, it is likely to have a PermGen space error.

(3). JVM memory limit (maximum value)
First of all, the JVM memory is limited to the actual maximum physical memory (nonsense!, huh). Assuming that the physical memory is infinite, the maximum value of the JVM memory has a great relationship with the operating system. To put it simply, although the controllable memory space of a 32-bit processor has 4GB, the specific operating system will impose a limit, which is generally 2GB-3GB (generally 1.5G-2G under Windows system and 1.5G-2G under Linux system). 2G-3G), and processors above 64bit will not have restrictions.

2. Introduction of three kinds of memory overflow exceptions

1. OutOfMemoryError: Java heap space heap overflow The main problem of

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

2. OutOfMemoryError: PermGen space non-heap overflow (permanent storage area overflow)

This kind of error is common when the web server precompiles the JSP. If your WEB APP uses a large number of third-party jars, the size of which exceeds the default size of jvm (4M), then this error message will be generated. If the web app uses a large number of third-party jars or the application has too many class files and the MaxPermSize is set to a small value, it will also cause excessive memory usage and overflow, or the tomcat will not clear the front during hot deployment. The loaded environment will only change the context to the newly deployed one, and there will be more and more non-heaped content.

3. OutOfMemoryError: unable to create new native thread. Unable to create new thread

This phenomenon is relatively rare and strange, mainly related to the ratio of jvm to system memory. This weirdness is because the JVM has been allocated a lot of memory by the system (say 1.5G) and it takes at least half of the available memory.


3. Java JVM memory configuration

1. There are four parameters for JVM memory allocation settings:

-Xmx Java Heap maximum value, the default value is 1/4 of the physical memory;

-Xms Java Heap initial value, the server-side JVM is best to use -Xms and -Xmx is set to the same value, the development and testing machine JVM can keep the default value;

-Xmn Java Heap Young area size, if you are not familiar with it, it is better to keep the default value;

-Xss The stack size of each thread, if you are not familiar with it, it is better to keep the default value;

- XX:PermSize: Set the permanent storage area of ​​the memory;
-XX:MaxPermSize: Set the permanent storage area of ​​the maximum memory;

-XX:PermSize: Set the permanent storage area of ​​the memory;

-XX:NewSize: Set the new life of the JVM heap The default size of the generation';

-XX:MaxNewSize: set the maximum size of the 'new generation' of the JVM heap;



===========View tomct memory usage =========== ====
Edit the tomcat-users.xml file and
configure
<role rolename="manager-gui"/>
  <user username="admin" password="admin" roles="manager-gui"/> to
restart tomcat
and run http:/ /localhost:8080/
click server status
Enter admin admin

to check the tomcat memory usage




JVM memory area is divided into Eden Space, Survivor Space, Tenured Gen, Perm Gen explained 2014-11-12 16:53:53
Classification: Java
jvm area is generally divided into two categories, heap area and non-heap Area. The heap area is further divided into: Eden Space (Eden), Survivor Space (Survivor Area), Tenured Gen (Old Age - Pension Area). The non-heap area is divided into: Code Cache (code cache area), Perm Gen (permanent generation), Jvm Stack (java virtual machine stack), Local Method Statck (local method stack).

The HotSpot virtual machine GC algorithm adopts a generational collection algorithm:

1. After a person (object) comes out (new comes out), he will live a carefree life in Eden Space (Eden) until the arrival of GC breaks their peaceful life. The GC will ask the situation of each object one by one, whether there is any money (a reference to this object), because the GC wants to make money, only the rich can extort money. Then the rich will enter the Survivor Space (survivor area), and the poor will be killed directly.

2. It is not guaranteed that the person is safe after entering the Survivor Space (survivor area), but at least you can live for a while. The GC will regularly (can be customized) extort these people. The billionaire gives money every time. The GC is very satisfied and let him enter the Genured Gen (pension area). Households with 10,000 yuan have no money if they can’t stand a few extortions. GC sees no value, so they just kill them.

3. People who enter the pension area can basically guarantee their personal safety, but some billionaires will also squander into poor people. As long as the money is gone, the GC will still be killed.

The purpose of partitioning: in the new area, since many objects are generated and most of them are dying, the mark-cleaning algorithm is directly used. The old-age care area has a strong vitality, so a replication algorithm is used, and different algorithms are used for different situations.

In the non-heap area, Perm Gen holds the definitions of classes and methods, and the jvm Stack area holds references to method parameters and local variables.

The above is from: http://lhc1986.iteye.com/blog/1421832

The following is from: http://www.cnblogs.com/xhr8334/archive/2011/12/01/2270994.html





GC working mechanism
SUN's jvm memory pool It is divided into the following parts:
Eden Space (heap)
memory is initially allocated from this thread pool to most objects.

Survivor Space (heap)
is used to save objects that have not been collected after garbage collection in the eden space memory pool.

Tenured Generation (heap)
is used to keep objects that have existed in the survivor space memory pool for a period of time.

Permanent Generation (non-heap)
holds the virtual machine's own static (reflective) data, such as class and method objects. The Java virtual machine shares these class data. This area is split into read-only and write-only.

Code Cache (non-heap)
The HotSpot Java Virtual Machine includes a memory for compiling and saving native code, called the "code cache".

To put it simply, the memory recycling process of JVM is as follows:
objects are created in Eden Space. When Eden Space is full, gc scans all objects in Eden Space once, and copies all valid objects to the first one. Survivor Space, while releasing the space occupied by invalid objects. When the Eden Space becomes full again, the moving program is started to copy the valid objects in the Eden Space to the second Survivor Space, and at the same time, the valid objects in the first Survivor Space are also copied to the second Survivor Space. If the valid objects filled into the second Survivor Space are referenced by objects in the first Survivor Space or Eden Space, then these objects are long-lived, and at this time these objects will be copied to Permanent Generation.
If the garbage collector cannot collect enough space according to this small adjustment, it will run the Full GC, at which time the jvm gc stops all the threads running in the heap and performs the cleanup action.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326401029&siteId=291194637