JVM Heap Memory tuning

Source: Internet
Author: User
Tags xms

Recently my server because of the TOMCAT7 put more projects so JVM memory is not enough, reported outofmem Java heap size exception. It is clear that the Tomcat JVM heap memory needs to be reset.

My server is buying Aliyun services, single core 2G memory, because it doesn't need to run too complex for applications, so that's enough.

The current generation size of 87M is seen from the GC log and the old age is 126M. To reset the JVM heap memory size, you need to set the following parameters:

1.-xmx,-XMS, specifies the initial and maximum values of the Cenozoic and old age space sizes. -XMS Specifies the initial value,-XMX specifies the maximum value. The two parameters specify the size of the Java heap memory. The JVM adjusts the size between the initial value and the maximum value, depending on the situation. However, to avoid JVM resizing, the two values can be set as large, because the Java heap memory adjustment necessarily accompanies the full GC, resulting in application pauses and delays. If you are very concerned about throughput and latency, be aware that both values must be set to the same value.

2. The size of the Cenozoic can be set by the following parameters:

-XX:NEWSIZE=<N>[G|M|K], sets the initial value of the Cenozoic, is also the minimum value, n is the size to be set, G,m,k is the unit

-xx:maxnewsize=<n>[g|m|k], the Cenozoic maximum value

-XMN<N>[G|M|K] Sets the initial value of the Cenozoic, the minimum value, the maximum value. This parameter can set the Cenozoic to a fixed value and will not change as the heap size adjusts. That is, if the-xmx and the-xms are not set to the same value, then the Cenozoic size will not change under the action of-XMN.

3. The size of the old age is equal to the value of-XMX and-xms set minus the Cenozoic value, so there is no parameter set separately.

4. Permanent Generation Size setting:

-XX:PERMSIZE=<N>[G|M|K], set the initial value of the permanent generation, and also the minimum value

-XX:MAXPERMSIZE=<N>[G|M|K], setting the maximum value of a permanent generation

The change in the size of the permanent generation also causes a full GC, so if you do not want to have the GC too often, set the initial and maximum values of the permanent generation to the same value.

After studying the parameters that need to be set up, we begin to study how to set

The memory is sufficient for the JVM, and it does not allow other applications on the server to have no memory available, view the server's current memory usage, the server's memory usage is around 60%, so there is room to allocate to the JVM to use, after all, Tomcat is the main application on my server.

The principle of JVM optimization is that the minor GC reclaims most of the memory. If the minor GC cannot be recycled, it will go into the old age, and if the old age is full, it will be a fully GC, how to fully GC or not free enough space to allocate new

The memory will run out of the memery exception. In addition, a full GC is also triggered by the permanent generation, and it does not allow sufficient space for the permanent generation to be allocated to the new object and out of the memery exception. So the size of the old age and the permanent generation depends on how much active data you have in your application. Active data refers to data that is still left in memory after the GC, which can cause you to not allocate memory to new objects if it fills up the memory space. So analyzing the active data size of an application in a stable state can help set the JVM heap memory size.

The active data size can be known by analyzing the use of each region in the JVM heap after full GC. Here needs to be studied including the active data size of the older generation and the active data size of the permanent generation.

Java Heap Size calculation law
Space command-line Options occupy multiple
Java heap -xmx and-XMS 3-4 times full GC space footprint in old age
Permanent generation -xx:permsize
1.2-1.5 times times full GC permanent generation space consumption
Cenozoic -xmn 1-1.5 times times the full GC space footprint after the old age
Old age Java heap size minus Cenozoic size 2-3 times the old age space footprint

The GC log shows that the old age occupies 124M after the full GC, and that the permanent generation takes up 100M after full GC, and that it is more likely to be applied to Tomcat in the future, so it is larger:



Reboot Tomcat after Setup to see that the current server's memory usage is 85% acceptable. It is also considered the full use of this single core 2G server.

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.