A method for implementing Tomcat memory overflow and sizing _java

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

One, Tomcat memory Setup problem collection

A Java.lang.OutOfMemoryError exception occurs when you use a Java program to query a large amount of data from a database or when the application server (such as Tomcat, jboss,weblogic) loads a jar package. This is mainly due to insufficient memory on the application server. This anomaly is often the case in the following cases (for example, for the Tomcat environment, other Web servers, such as Jboss,weblogic, are the same):

1. Java.lang.OutOfMemoryError:PermGen Space

PermGen space is the full name of the permanent Generation, refers to the memory of the permanent storage area outofmemoryerror:permgen spaces. From the text is the memory overflow, the solution is to increase memory. Why is there a memory overflow, because this memory is primarily stored in class and meta information by the JVM, class is placed in the PermGen space area when it is load, and is different from the heap area where the instance is stored, GC (garbage Collection) does not clean the PermGen space during the run of the main program, so if your app will load a lot of classes, it is likely that PermGen space error will occur. This error is common when the Web server is pre compile the JSP. If your web app uses a large number of third-party jars that are larger than the JVM default size (4M), this error message is generated.

Workaround: Manually set the MaxPermSize size

A. If Tomcat is started as a bat, the following settings are set:

Modify Tomcat_home/bin/catalina.sh

In the "echo" Using catalina_base: $CATALINA _base "", add the following line:

Java_opts= "-server-xx:permsize=64m-xx:maxpermsize=128m

B. If Tomcat is registered as a Windows service and started in services, you will 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, A red callout (such as htfty) in the directory address needs to be modified according to different circumstances, registering the name of the Windows service for the Tomcat service. You can see the Jvmms and jvmmx items, where Jvmms set the minimum memory usage parameters, JVMMX set the maximum memory usage parameters. Set the value 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 will reduce the memory consumption of the jar document.

2. Java.lang.OutOfMemoryError:Java Heap Space

The JVM heap is set up to refer to the set of memory space that the JVM can use to deploy during the runtime of the Java program. The JVM automatically sets the value of 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. 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 for GC and the available heap size is less than 2%.

Workaround: Manually set heap Size

A. If Tomcat is started as a bat, the following settings are set:

Modify Tomcat_home/bin/catalina.sh

In the "echo" Using catalina_base: $CATALINA _base "", add the following line:

Java_opts= "-server-xms800m-xmx800m-xx:maxnewsize=256m"

B. If Tomcat is registered as a Windows service and started in services, you will 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, A red callout (such as htfty) in the directory address needs to be modified according to different circumstances, registering the name of the Windows service for the Tomcat service. You can see the Jvmms and jvmmx items, where Jvmms set the minimum memory usage parameters, JVMMX set the maximum memory usage parameters. Set the value of the Jvmms and JVMMX entries and restart the Tomcat server to take effect.

Tip: Heap Size is not greater than 80% of the available physical memory, typically to set the-XMS and-XMX options to the same, and-xmn to the-XMX value of 1/4.

Second, Tomcat itself cannot run directly on the computer, and relies on a hardware-based 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. For example, when the application uses the maximum memory, the JVM is going to do the garbage collection action, releasing some memory occupied. So if you want to adjust the initial and maximum memory for Tomcat startup, you need to declare to the JVM that normal Java programs can adjust the initial and maximum memory of the application through-XMS-XMX: The size of the two values is generally set as needed. The size of the initialization heap performs the size of the memory that the virtual machine requests to the system at startup. Generally speaking, this parameter is not important. However, some applications in a large load of the case will be a sharp use of more memory, at this time this parameter is very important, if the virtual machine is set up at the start of 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 the-XMS and-xmx to be the same size, while the maximum of the heap is limited by the physical memory used by the system. Typically, applications that use large amounts of data use persistent objects, and memory usage is likely to grow rapidly. When the application needs more memory than the maximum of the heap, the virtual machine prompts for a memory overflow and causes the application service to crash. Therefore, the maximum value of the generic recommended heap is set to 80% of the maximum available memory.

Tomcat can use the default memory of 128MB, in larger application projects, this memory is not enough, need to be scaled up. There are several ways to choose:

The first method:

Under Windows, under 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 available '

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

java_opts= '-xms256m-xmx512m '

Indicates that the initialization memory is 256MB and the maximum memory available is 512MB.

The second method: set Variable name in environment variable: java_opts variable value:-xms512m-xmx512m

The third method: The first two methods are for Catalina.bat in the bin directory (such as direct decompression of Tomcat, etc.), but some installed version of Tomcat without Catalina.bat, this time can be used as follows, of course, this method is also the most common method: Open the Tomcathome /\bin/\tomcat5w.exe, click on the Java tab, and then you'll find that there are two of them: Initial memory pool and maximum memory pool. Initial memory Pool This is the size of the memory to initialize the settings. Maximum memory Pool This is the maximum size of the memory set, just press OK and restart Tomcat. You'll 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 the 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 actual time and frequency of garbage collection. If the heap is large in size, complete garbage collection can be slow, but the frequency is reduced. If you match the size of the heap with the need for memory, the collection will be complete quickly, but will be more frequent. The purpose of the heap sizing is to minimize the time spent in garbage collection to maximize the processing of customer requests for a specific period of time. In the benchmark, to ensure the best performance, the heap size is set to ensure that garbage collection does not occur throughout the benchmarking 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, examine the verbose output of garbage collection, and study the impact of garbage collection parameters on performance. Generally, you should use 80% of the physical memory as the heap size. When adding the processor, remember to increase the memory, because the allocation can be done in parallel, and garbage collection is not parallel.

A place to note: It is recommended to reduce the maximum value of memory to the minimum value of the difference, otherwise it will waste a lot of memory, the lowest value increases, the highest value can be arbitrarily set, but to the actual physical memory, if the memory settings are too large, such as set the 512M maximum memory, but if there is no 512M available memory, Tomcat will not boot, and there may be memory being reclaimed by the system, terminating the process

The above tomcat memory overflow and size adjustment of the implementation method is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.