1. Collection of tomcat memory settings issues
When using Java programs to query a large amount of data from the database or loading a jar package by application servers (such as tomcat, jboss, weblogic) will appear. The java.lang.OutOfMemoryError exception will appear. This is mainly caused by insufficient memory in the application server. This exception often has the following situations (taking the tomcat environment as an example, other WEB servers such as jboss, weblogic, etc. are the same):
1. java.lang.OutOfMemoryError: PermGen space
The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of memory OutOfMemoryError: PermGen space. From a text perspective, it is memory overflow, and the solution is to increase the memory. Why does memory overflow? This is because this memory is mainly stored by the JVM in Class and Meta information. Class is placed in the PermGen space area when it is loaded. It is different from the Heap area where Instance is stored. GC (Garbage Collection) will not clean up the PermGen space during the main program run. Therefore, if your APP will LOAD a lot of CLASS, it is very likely that a PermGen space error will occur. This error is common when the web server pre-compiles the JSP. If you use a large number of third-party jars under your WEB APP, the size exceeds the default size of jvm (4M), then this error message will be generated.
Solution: Manually set the MaxPermSize size
a. If tomcat is started in bat mode, the following settings are:
Modify TOMCAT_HOME/bin/catalina.sh
Add the following line to "echo "Using CATALINA_BASE:$CATALINA_BASE":
JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m
b. If tomcat is registered as a Windows service and started in services, the corresponding key value in the registry needs to be modified.
Open the registry and find the directory HKEY_LOCAL_MACHINE/SOFTWARE/Apache Software Foundation/Procrun 2.0/htfty/Parameters/Java. The red marked (such as htfty) in the directory address needs to be modified according to different situations, and register the tomcat service as the name of the windows service. You can see the JvmMs and JvmMx items, where JvmMs sets the smallest memory usage parameters, and JvmMx sets the largest memory usage parameters. Set the values of JvmMs and JvmMx items, and restart the tomcat server to take effect.
Recommendation: Move the same third-party jar file to the tomcat/shared/lib directory, so as to reduce the repeated memory usage of jar documents.
2. java.lang.OutOfMemoryError: Java heap space
The settings of the JVM heap refer to the settings of the memory space that the JVM can provision during the Java program operation. When the JVM starts, it will automatically set the value of Heap size. Its initial space (i.e. -Xms) is 1/64 of physical memory, and the maximum space (-Xmx) is 1/4 of physical memory. You can use the -Xmn -Xms -Xmx and other options provided by the JVM to set it. The size of Heap size is the sum of Young Generation and Tenured Generaion. This exception message will be thrown if 98% of the time is used for GC and the available Heap size is less than 2%.
Solution: Set Heap size manually
a. If tomcat is started in bat mode, the following settings are:
Modify TOMCAT_HOME/bin/catalina.sh
Add the following line to "echo "Using CATALINA_BASE:$CATALINA_BASE":
JAVA_OPTS="-server -Xms800m -Xmx800m-XX:MaxNewSize=256m"
b. If tomcat is registered as a Windows service and started in services, the corresponding key value in the registry needs to be modified.
Open the registry and find the directory HKEY_LOCAL_MACHINE/SOFTWARE/Apache Software Foundation/Procrun 2.0/htfty/Parameters/Java. The red marked (such as htfty) in the directory address needs to be modified according to different situations, and register the tomcat service as the name of the windows service. You can see the JvmMs and JvmMx items, where JvmMs sets the smallest memory usage parameters, and JvmMx sets the largest memory usage parameters. Set the values of JvmMs and JvmMx items, and restart the tomcat server to take effect.
Tip: The maximum Heap Size should not exceed 80% of the available physical memory. Generally, set the -Xms and -Xmx options to the same, while -Xmn is 1/4 of the -Xmx value.
2. Tomcat itself cannot run directly on the computer, and it needs to rely on the operating system based on the hardware and a Java virtual machine. When the JAVA program is started, the JVM will allocate an initial memory and a maximum memory to the application. This initial memory and maximum memory will affect the performance of the program to a certain extent. For example, when an application uses the maximum memory, the JVM must first do garbage collection and release some occupied memory. Therefore, if you want to adjust the initial memory and maximum memory of Tomcat when starting, you need to declare it to the JVM. Generally, JAVA programs can adjust the initial memory and maximum memory of the application through -Xms -Xmx when running: the sizes of these two values are generally set according to needs. The initialization heap size executes the size of memory the virtual machine requests to the system at startup. Generally speaking, this parameter is not important. However, some applications will rapidly occupy more memory under large loads. At this time, this parameter is very important. If the memory used is set to be small when the virtual machine is started and many objects are initialized in this case, the virtual machine must repeatedly increase the memory to satisfy the use. For this reason, we generally set -Xms and -Xmx to be the same size, and the maximum value of the heap is limited by the physical memory used by the system. Generally, applications with large amounts of data will use persistent objects, and memory usage may grow rapidly. When the memory required by the application exceeds the maximum value of the heap, the virtual machine will prompt memory overflow and cause the application service to crash. Therefore, it is generally recommended to set the maximum value of the heap to 80% of the maximum value of available memory.
Tomcat can use memory by default is 128MB. In larger application projects, this memory is not enough and needs to be increased. There are several ways to choose:
The first method:
Under Windows, in the file /bin/catalina.bat, Unix, in front of the file /bin/catalina.sh, add the following settings:
JAVA_OPTS='-Xms【Initialize memory size】-Xmx【Maximum memory that can be used】'
The values of these two parameters need to be increased. For example:
JAVA_OPTS='-Xms256m -Xmx512m'
It means that the initialization memory is 256MB and the maximum memory that can be used is 512MB.
The second method: Set the variable name in the environment variable: JAVA_OPTS variable value: -Xms512m -Xmx512m
The third method: The first two methods are aimed at the situation where catalina.bat is in the bin directory (such as Tomcat that is directly decompressed, etc.), but some installation versions of Tomcat do not have catalina.bat. At this time, the following method can be used. Of course, this method is also the most general method: Open tomcatHome//bin//tomcat5w.exe, click on the Java tab, and you will find that there are two items: Initial memory pool and Maximum memory pool.Initial memory pool This is the memory size of the initialization setting. Maximum memory pool This is the maximum memory size. After setting the setting, press OK and restart TOMCAT. You will find that the memory available to jvm in tomcat has changed.
Another thing to consider is the garbage collection mechanism provided by Java. The heap size of the virtual machine determines the time and frequency of the virtual machine spending on collecting garbage. The acceptable speed of garbage collection is related to the application and should be adjusted by analyzing the time and frequency of actual garbage collection. If the heap size is large, then complete garbage collection will be slow, but the frequency will be reduced. If you make the heap size consistent with the memory needs, the complete collection will be quick, but it will be more frequent. The purpose of resizing the heap is to minimize the time of garbage collection to maximize processing of customer requests within a specific time. During benchmarking, in order to ensure the best performance, the heap size must be set to be large to ensure that garbage collection does not occur during the entire benchmarking process. If the system spends a lot of time collecting garbage, reduce the heap size. A complete garbage collection should not exceed 3-5 seconds. If garbage collection becomes a bottleneck, then the size of the generation needs to be specified, the detailed output of garbage collection is checked, and the impact of garbage collection parameters on performance is studied. Generally speaking, you should use 80% of physical memory as the heap size. When adding the processor, remember to increase the memory, because allocations can be performed in parallel, while garbage collection is not in parallel.
One thing to note: It is recommended to narrow the difference between the highest value and the lowest value, otherwise a lot of memory will be wasted. The lowest value is increased, and the highest value can be set at will, but it must be based on the actual physical memory. If the memory setting is too large, for example, the maximum memory is set, but if there is no 512M available memory, Tomcat cannot be started. There may be a situation where the memory is recycled by the system and terminated the process.
The above method of implementing TOMCAT memory overflow and size adjustment is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.