JVM memory parameter settings

Source: Internet
Author: User
Tags xms

1. Heap configuration and garbage collection
Java memory is divided into three areas: young, old, and permanent. Young stores the just-instantiated object. When the area is filled up, GC moves the object to the old area.
The first two areas are usually called heap, and the permanent area is responsible for saving the reflection object and class meta information.
The exception of memory overflow in the heap area is: Java. Lang. outofmemoryerror: Java heap space,
The memory overflow in the permanent area is: outofmemoryerror: permgen space.

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

-XMS Initial heap size
-Xmx Java heap maximum
-Xmn Heap size of young generation

JVM has two GC threads. The first thread is responsible for revoking heap's young zone. When the heap is insufficient, the second thread traverses the heap 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-XMS cannot be set too large, because the second thread is forced to run will reduce JVM performance.
Why does GC occur frequently in some programs? There are the following reasons:
L The program calls system. GC () or runtime. GC ().
L some middleware software calls their own GC methods. In this case, you need to set parameters to disable these GC methods.
L Java heap is too small. Generally, the default heap value is very small.
L objects are frequently instantiated and release objects. In this case, try to save and reuse objects, for example, using stringbuffer () and string ().
If you find that after each GC, the remaining heap space will be 50% of the total space, which indicates that your heap is healthy. Many Java programs on the server end have a better free space of 65% after each GC.
Experience:
1. It is best to set-XMS and-xmx to the same value on the server JVM. To optimize GC, it is recommended that the-xmn value be equal to 1/3 [2] of-xmx.
2. It is best for a GUI program to run GC every 10 to 20 seconds and complete GC every time within half a second [2].
 
Note:
1. Increasing the heap size reduces the GC frequency, but increases the GC time. When GC is run, all user threads are paused, that is, Java applications do not work during GC.
2. The heap size does not determine the memory usage of the process. The memory usage of a process must be greater than the value defined by-xmx, Because Java allocates memory for other tasks, such as the stack of each thread.

2. stack settings
Each thread has its own stack.

-XSS Stack of each thread

The stack size limits the number of threads. If the stack is too large, the memory may leak. -The XSS parameter determines the stack size, for example,-xss1024k. If the stack is too small, the stack may leak.

3. Permanent Zone
The outofmemoryerror: permgen space that may be encountered during program release. When the class is loaded, it will be placed in permgen space,
Unlike the heap region of the storage instance, GC (garbage collection) does not
Permgen space is cleaned up. Therefore, if your application contains many classes, the permgen space error may occur,
This error is common when the web server pre-compile the JSP.
Use-XX: permsize = 64 m-XX: maxpermsize = m to adjust.

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


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.