Detailed memory settings in the Java Virtual Machine (JVM)

Source: Internet
Author: User
Tags xms

In some slightly larger applications, the memory settings of the Java Virtual Machine (JVM) are particularly important, and the GC (garbage collection) setting is the first step in trying to achieve good efficiency in the project.

PermGen space: The full name is permanent Generation space. is a permanently saved area for storing class and meta information, and class is placed in the area of heap space when it is loaded: storage instance.

The GC (garbage Collection) should not clean up the PermGen space, so if your app will load many classes, you will likely have PermGen space error

Java Heap is divided into 3 zones

Young saves the object that was just instantiated. When the area is filled, the GC moves the object to the old area. The permanent area is responsible for saving the reflected object, which is not discussed in this article.

The heap allocation for the JVM can be set using the-x parameter,

Initial Heap Size

Java Heap Maximum Value

The heap size of young generation

JVM has 2 GC threads
The first thread is responsible for recovering the young area of the heap
The second thread traverses the heap when the heap is insufficient, and upgrades the young area to the older area

The size of the older area is equal to-xmx minus-xmn, and the value of the-XMS cannot be set too large because the second thread is forced to run to degrade the performance of the JVM.
Why do some programs frequently occur in GC?

There are the following reasons:
1. System.GC () or RUNTIME.GC () is called within the program.
2. Some middleware software calls its own GC method, at which time you need to set parameters to prohibit these GC.
The heap of 3.Java is too small, and the default heap values are generally small.
4. Frequent instantiation of objects, release objects try to save and reuse objects at this time, such as using StringBuffer () and string ().

If you find that the remaining space in the heap will be 50% of the total space after each GC, it means that your heap is in a healthy state, and many server-side Java programs have a better 65% of the space left after each GC


1. The server-side JVM is best to set-XMS and-xmx to the same value. In order to optimize GC, it is best to make-xmn value approximately equal to 1/3 of-XMX.
2. A GUI program is best to run a GC every 10-20 seconds, each time within half a second.


1. Increasing the size of the heap decreases the frequency of the GC, but it also increases the time of each GC. And when the GC runs, all the user threads are paused, that is, during the GC, the Java application does not do any work.
2. The heap size does not determine the amount of memory used by the process. The memory usage of the process is greater than the value defined by-XMX because Java allocates memory for other tasks, such as the stack for each thread.

Stack settings
Each thread has his own stack.

Stack size per thread

The size of the stack limits the number of threads. If the stack is too large, it will cause memory leaks. The-XSS parameter determines the stack size, such as-xss1024k. If the stack is too small, it can also cause the stack to spill.

Hardware environment

The hardware environment also affects the efficiency of the GC, such as the type of machine, memory, swap space, and number of CPUs.
If your program needs to create many transient objects frequently, it will cause the JVM to frequent GC. In this case you can increase the memory of the machine to reduce the use of swap space.

4 GC Types

1, the first is a single-threaded GC, is also the default GC, the GC applies to a single CPU machine.
2, the second type of throughput GC, is a multi-threaded GC, applicable to multi-CPU, using a large number of threads of the program. The second GC is similar to the first GC, except that the GC is multithreaded in the collection of young, but in the old area it is still single-threaded, as in the first type. The-XX:+USEPARALLELGC parameter starts the GC.
3. The third type of concurrent low Pause GC, similar to the first, applies to multiple CPUs and requires shortening the time that the GC causes the program to stall. This GC can run the application at the same time as the recycle in the old area. The-XX:+USECONCMARKSWEEPGC parameter starts the GC.
4. The fourth type is incremental low Pause GC, which is suitable for reducing the time that the GC causes the program to stall. This GC can reclaim part of the old object while recovering from the young area. The-XINCGC parameter starts the GC.

Single-File JVM memory settings

The default Java virtual machine size is relatively small, when the big data processing, Java will be error: Java.lang.OutOfMemoryError.
To set the JVM memory method, for a separate. Class, you can set the JVM memory of the test runtime with the following method.
java-xms64m-xmx256m Test
-XMS is the size of the set memory initialization
-XMX is to set the maximum amount of memory that can be used (preferably not exceeding the physical memory size)

Tomcat Boot JVM Memory settings


In the/usr/local/apache-tomcat-5.5.23/bin directory add: java_opts= '-xms512m-xmx1024m ' to add "M" description is MB, otherwise it is KB, Out of memory is reported when Tomcat is started.
-XMS: Initial value
-XMX: Maximum Value
-XMN: Minimum value of windows
Join at the front of the Catalina.bat
Set java_opts=-xms128m-xmx350m if the Tomcat,ok setting is in effect with startup.bat. Enough to allocate 200M of memory successfully. But if you don't do startup.bat start Tomcat, it's Using Windows system services to start the Tomcat service, the settings above do not take effect, which means that set java_opts=-xms128m-xmx350m does not work. Allocate 200M of memory on the above to Oom. The Windows service performs bin\tomcat.exe. He reads the value in the registry instead of the Catalina.bat setting. Workaround:

Modify the registry Hkey_local_machine\software\apache software Foundation\tomcat Service manager\tomcat5\parameters\javaoptions

The original value is

-dcatalina.home= "C:\ApacheGroup\Tomcat 5.0"
-djava.endorsed.dirs= "C:\ApacheGroup\Tomcat 5.0\common\endorsed"
-xrs Join-xms300m-xmx350m

Restart Tomcat service, set to take effect

WebLogic Starting JVM Memory settings

In WebLogic, you can set the size of each domain's virtual memory in Startweblogic.cmd, with the default setting in Commenv.cmd.


The default memory available is 64MB
$JBOSSDIR $/bin/run.config
java_opts = "-server-xms128-xmx512"


In your directory, type
256m represents the minimum JVM heap memory value
512M indicates JVM heap memory max


Go to the console to set up: Application Server > Server1 > Process definition > Java virtual machine

Detailed memory settings in the Java Virtual Machine (JVM)

Related Article

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: 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.