Detailed description of memory settings in Java Virtual Machine (JVM)

Source: Internet
Author: User
Tags xms

In some large-scale applications, memory settings of Java Virtual Machine (JVM) are particularly important. To achieve good efficiency in the project, GC (garbage collection) settings are the first step.

Permgen Space: The full name is permanent generation space. That is to say, it is a permanent storage area, used to store class and meta information. When the class is loaded, it is placed in this area heap space: store instance.

GC (garbage collection) should not clean the permgen space. Therefore, if your app loads many classes, the permgen space error may occur.

Java heap is divided into three zones
1. Young
2. Old
3. Permanent

Young stores the just-instantiated object. When the area is filled up, GC moves the object to the old area. The permanent area stores the reflection object. This section does not discuss this area.

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 2 GC threads
The first thread is responsible for revoking heap's young Zone
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-XMS cannot be set too large, because the second thread is forced to run will reduce JVM performance.
Why?ProgramFrequent GC?

There are the following reasons:
1. The program calls system. GC () or runtime. GC ().
2. Some middleware software calls its own GC method. In this case, you need to set parameters to disable these GC methods.
3. Java heap is too small. Generally, the default heap value is very small.
4. Frequently instantiate objects. The release objects should be saved and reused at this time, for example, using stringbuffer () and string ().

If you find that the remaining heap space after each GC is 50% of the total space, it indicates that your heap is healthy, many server-side Java programs have a better 65% free space 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 approximately 1/3 of-xmx.
2. It is best to run a GC every 10 to 20 seconds for a GUI program to be completed within half a second each time.

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.

Stack settings
Each thread has its own stack.

-XSS
Stack size 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.

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 usage of swap space.

Four types of GC

1. The first type is single-thread GC, which is also the default GC. This GC is applicable to single-CPU machines.
2. The second type is throughput GC, which is a multi-threaded GC. It is suitable for programs with multiple CPUs and a large number of threads. The second type of GC is similar to the first type of GC. The difference is that the GC is multi-threaded in the young area, but in the old area and the first one, the single thread is still used. -XX: + useparallelgc parameter to start the GC.
3. The third type is concurrent low pause GC, which is similar to the first type. It is suitable for multiple CPUs and requires shortening the time of program stagnation caused by GC. This GC can be used to run applications in the old zone. -XX: + useconcmarksweepgc parameter to start the GC.
4. The fourth type is incremental low pause GC, which is applicable to the requirement to shorten the time of program stagnation caused by GC. This GC can be used to reclaim some old objects in the young area. -The xincgc parameter starts the GC.

Set the JVM memory for a single file

The default Java Virtual Machine is small in size. When processing big data, Java reports an error: Java. Lang. outofmemoryerror.
The JVM memory setting method. For a separate. class, you can use the following method to set the JVM memory for test runtime.
Java-xms64m-xmx256m Test
-XMS is used to set the memory initialization size.
-Xmx is used to set the maximum memory available (it is best not to exceed the physical memory size)

Tomcat startup JVM memory settings

Linux:

Catalina in the/usr/local/Apache-Tomcat-5.5.23/bin directory. sh Add: java_opts = '-xms512m-xmx1024m'. To add "M", it indicates that it is mb. Otherwise, it is KB. When Tomcat is started, the system reports insufficient memory.
-XMS: Initial Value
-Xmx: Maximum Value
-Xmn: minimum Windows
Add at the beginning of Catalina. bat
Set java_opts =-xms128m-xmx350m if startup is used. bat starts tomcat, and OK settings take effect. MB of memory can be allocated successfully. however, if you do not execute startup. bat starts Tomcat but uses the Windows System Service to start the Tomcat service. The above settings do not take effect, that is, set java_opts =-xms128m-xmx350m does not work. the above MB memory is allocated to oom .. the Windows service runs bin \ tomcat.exe. he reads the value in the Registry instead of Catalina. bat settings. solution:

Modify the Registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ Tomcat Service Manager \ tomcat5 \ Parameters \ javaoptions

Original Value:

-Dcatalina. Home = "C: \ apachegroup \ Tomcat 5.0"
-Djava. endorsed. dirs = "C: \ apachegroup \ Tomcat 5.0 \ common \ endorsed"
-Add xrs-xms300m-xmx350m

Restart the Tomcat service and the settings take effect.

JVM startup settings for WebLogic

In WebLogic, you can set the virtual memory size of each domain in startweblogic. cmd. The default setting is in commenv. cmd.

JBoss

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

Eclipse

In the directory, Type
Eclipse.exe-vmargs-xms256m-xmx512m
M indicates the minimum JVM heap memory size.
M indicates the maximum JVM heap memory

WebSphere

Go to the console to set: Application Server> server1> process definition> Java Virtual Machine

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