Tomcat Memory Overflow Detailed

Source: Internet
Author: User
Tags app service garbage collection memory usage jboss tomcat tomcat server xms

One, Tomcat memory setup issues

A Java.lang.OutOfMemoryError exception occurs when you use a Java program to query large amounts of data from a database or an application server (such as Tomcat, jboss,weblogic) loads a jar package. This is mainly due to insufficient memory on the application server. The exceptions are often as follows (for example, for the Tomcat environment, other Web servers, such as Jboss,weblogic, 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 the text is memory overflow, the solution is to increase the memory. Why memory overflow, this is because the memory is mainly stored by the JVM class and meta information, class is loaded when the PermGen space is placed in the area, it is stored in the instance heap area, GC (garbage Collection) does not clean up permgen space during the main program run time, so if your app will load a lot of classes, it is likely that PermGen space error is occurring. This error is common when the Web server pre-compile the JSP. If you have a large number of third-party jars under your web app that are larger than the JVM's default size (4M), this error message will be generated.

Workaround: Manually set the MaxPermSize size

A. If Tomcat is started in bat mode, the following settings are set:

Modify Tomcat_home/bin/catalina.sh

Add the following line to the "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, you need to modify the corresponding key values in the registry.

Open the registry, locate the directory Hkey_local_machine\software\apache software Foundation\procrun 2.0\htfty\parameters\java, The red label in the directory address (such as htfty) needs to be modified according to different circumstances to register the Tomcat service as the name of the Windows service. You can see the Jvmms and jvmmx entries, where Jvmms sets the minimum memory usage parameters, JVMMX sets the maximum memory usage parameters. Set the values of the Jvmms and JVMMX entries and restart the Tomcat server to take effect.

Recommendation: Move the same third-party jar files to the Tomcat/shared/lib directory, which can reduce the memory consumption of the jar document.

2. Java.lang.OutOfMemoryError:Java Heap Space

The JVM heap setting is the set of memory space that the JVM can provision in the course of running the Java program. The JVM automatically sets the value of the heap size when it starts, and its initial space (that is,-XMS) is 1/64 of the physical memory, and the maximum space (-XMX) is 1/4 of the physical memory. You can use options such as the-XMN-XMS-XMX provided by the JVM to set it up. The size of Heap size is the sum of young Generation and tenured generaion. This exception information is thrown in the JVM if 98% of the time is used for GC and the available heap size is less than 2%.

Workaround: Manually set the heap size

A. If Tomcat is started in bat mode, the following settings are set:

Modify Tomcat_home/bin/catalina.sh

Add the following line to the "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, you need to modify the corresponding key values in the registry.

Open the registry, locate the directory Hkey_local_machine\software\apache software Foundation\procrun 2.0\htfty\parameters\java, The red label in the directory address (such as htfty) needs to be modified according to different circumstances to register the Tomcat service as the name of the Windows service. You can see the Jvmms and jvmmx entries, where Jvmms sets the minimum memory usage parameters, JVMMX sets the maximum memory usage parameters. Set the values of the Jvmms and JVMMX entries and restart the Tomcat server to take effect.

Tip: The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4.

Second, Tomcat itself cannot run directly on the computer, it needs to rely on the hardware based on the operating system and a Java virtual machine. When the Java program starts, the JVM allocates an initial memory and maximum memory to the application. This initial memory and maximum internal presence will affect the performance of the program in some degree. For example, when the application uses the maximum memory, the JVM is going to do garbage collection first, freeing up some of the memory that is occupied. So if you want to adjust the initial memory and maximum memory on tomcat boot, you need to declare to the JVM that the generic Java program can run through-XMS-XMX to adjust the application's initial memory and maximum memory: the size of these two values is generally set as needed. The size of the initialized heap performs the amount of memory requested by the virtual machine to the system at startup. Generally speaking, this parameter is not important. However, some applications in the case of heavy load will take up more memory, when this parameter is very important, if the virtual machine is set up when the memory is relatively small and in this case there are many objects to initialize, the virtual machine must repeatedly increase the memory to meet the use. For this reason, we generally set-XMS and-xmx as large, and the maximum value of the heap is limited by the physical memory used by the system. Applications that use large amounts of data generally use persistent objects, and memory usage can grow rapidly. The virtual machine prompts for memory overflow when the application needs more memory than the heap, and causes the app service to crash. Therefore, it is generally recommended that the maximum heap value be set to 80% of the maximum available memory.

Tomcat can use 128MB of memory by default, and in larger applications, this memory is not enough and needs to be scaled up. There are several ways to choose from:

The first method:

Under Windows, under File/bin/catalina.bat,unix, in front of file/bin/catalina.sh, add the following settings:

java_opts= '-xms ' Initialize memory size "-XMX" maximum memory that can be used "'

You need to increase the value of this two parameter. For example:

java_opts= '-xms256m-xmx512m '

Indicates that the initialized 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 for the bin directory under the Catalina.bat (such as the direct decompression of Tomcat, etc.), but some installed version of Tomcat is not catalina.bat, this time can use the following methods, of course, this method is the most common method: Open Tomcath Ome/\bin/\tomcat5w.exe, click on the Java tab, and you'll find two of them: Initial memory pool and maximum memory pool. Initial memory Pool This is the amount of RAM initialized to set. Maximum memory Pool This is the maximum size of the RAM set, press OK and then reboot Tomcat you will find that the memory available to the JVM in Tomcat has changed

Another thing to consider is the garbage collection mechanism provided by Java. The heap size of a virtual machine determines the time and frequency at which the virtual machine spends collecting garbage. The rate at which garbage collection can be accepted 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 the total garbage collection will be slow, but the frequency will be reduced. If you match the size of the heap to the needs of the memory, it will be collected very quickly, but more often. The purpose of sizing the heap is to minimize garbage collection time to maximize processing of customer requests over a specific time period.    In benchmarking, to ensure the best performance, the size of the heap to be large, to ensure that garbage collection does not occur throughout the baseline test process. If your system spends a lot of time collecting garbage, reduce the heap size. A complete garbage collection should be no more than 3-5 seconds. If garbage collection becomes a bottleneck, you need to specify the size of the generation, check the detailed output of the garbage collection, and investigate the performance impact of garbage collection parameters. Generally speaking, you should use 80% of the physical memory as the heap size. When adding processors, remember to increase the memory because allocations can be done in parallel, and garbage collection is not parallel.

One to note: It is recommended that the highest value of memory and the lowest value of the difference is reduced, otherwise it will waste a lot of memory, the lowest value, the highest value can be arbitrarily set, but according to the actual physical memory, if the memory settings are too large, such as setting the 512M maximum memory, but if there is no 512M of available memory, Tomcat cannot be started, and there may be memory being reclaimed by the system, terminating the process

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.