JVM Common parameter Settings good

Source: Internet
Author: User
Tags xms

1, the size of the heap can be set by-XMS and-XMX, generally set them to the same size, the purpose is to avoid re-sizing the heap after each garbage collection, such as-xms=2g-xmx=2g or-xms=512m-xmx=512m

2, the young generation size can be set through-XMN, such as-xmn=2g or-xmn512m, this value on the system performance, the sun is recommended to configure the entire heap of 3/8

3. Old generation size = Heap Size – younger generation size

4, the permanent generation or the long-term size can be controlled by-xx:permsize and-xx:maxpermsize

5,-xx:survivorratio control Eden and survivor memory consumption ratio, the default is 8;

If set NewRatio , then the entire heap space is the size of the 1/(NewRatio +1) Cenozoic space,-xx:newratio recommended 2 to 4.

If you specify both Newratio and newsize, you should use the larger one. Then, when the heap space is created, you can use the following expression to calculate the initial Cenozoic space size:

min(MaxNewSize, max(NewSize, heap/(NewRatio+1)))

Third, JVM memory overflow configuration

How can I print a heap dump when the JVM encounters an oom error? You can set the-xx:+heapdumponoutofmemoryerror parameter so that the JVM prints a dump when it detects an oom in memory. However, when the JVM startup parameter adds this parameter, the JVM fails to start: Unrecognized VM option ' +heapdumponoutofmemeryerror ', the problem is because the-xx:heapdumppath parameter configuration is not added. -xx:heapdumppath This parameter to set the dump file location. Set the JVM startup parameters to the following format:


Problem solved. When a memory overflow occurs in the JVM, the heap dump is printed under c:/

Common parameter settings

USEPARNEWGC the use of parallel GC for the Cenozoic;

Parallelgcthreads indicates that the number of threads in parallel is 8, generally the number of cores of CPU, when the number of cores is greater than 8 o'clock may not be applicable;

USECONCMARKSWEEPGC indicates the use of CMS GC for full GC;

-XX:+DISABLEEXPLICITGC indicates that explicit GC,SYSTEM.GC () is forbidden

-xx:+usecmscompactatfullcollection for CMS GC, which indicates that memory fragmentation is cleared while GC is in progress, but the total time of GC is extended

-XX:CMSINITIATINGOCCUPANCYFRACTION=80 for CMS GC, which means that recycling is done as soon as 80% usage is reached in older generations

Some configuration parameters for the heap information when the JVM crash:

Log heap information when-xx:errorfile=./xxx.log JVM crash

-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=./yyy.log Log heap information when the JVM is Oom


Trace Trace Parameters





-xloggc:log/gc.log//Specify the location of the GC log for file output

-XX:PRINTHEAPATGC//After each GC, the heap information is printed

Class Loading information



-ctrl +break Printing class information, usage of class


v. Loading of classesClass loading has three ways: 1, the command line starts the application time by the JVM initialization load 2, through the Class.forName () method dynamic loading 3, through the Classloader.loadclass () method dynamic loading three way difference is larger, see an example to understand: PublicclassHelloWorld {
PublicStaticvoidMain (string[] args)throwsclassnotfoundexception {
ClassLoader loader = HelloWorld.class. getClassLoader ();
//Use Classloader.loadclass () to load the class without executing the initialization block
Loader.loadclass ("Test2");
//using Class.forName () to load the class, the initialization block is executed by default
//Class.forName ("Test2");
//Use Class.forName () to load the class and specify ClassLoader, which does not perform static block initialization
//Class.forName ("Test2", false, loader);
} PublicclassTest2 {
System.out.println ("Static initialization blocks are executed!" ");
}  switch the loading mode separately, there will be different output results. The Http://lavasoft.blog.51cto.com/62575/184547/Java program runs in memory, when executed at the command line: the java HelloWorld command, The JVM loads the Helloworld.class into memory and forms a class object helloworld.class. The process is the class loading process: 1, look for the JRE directory, look for Jvm.dll, and initialize the jvm;2, generate a Bootstrap Loader (boot class loader); 3, Bootstrap Loader automatic loading extended Loader (Standard extension class loader) and set its parent Loader to Bootstrap Loader. 4. Bootstrap Loader automatically loads Appclass Loader (System ClassLoader) and sets its parent Loader to extended Loader. 5, finally by Appclass Loader load HelloWorld class.   above is the most general process of class loading. 1. Bootstrap Loader (startup ClassLoader): Loads the path or jar specified by the System.getproperty ("Sun.boot.class.path"). 2, Extended Loader (Standard extension class loader Extclassloader): Loads the path or jar specified by System.getproperty ("Java.ext.dirs"). When you run a program using Java, you can also specify its search path, for example: Java-djava.ext.dirs=d:\projects\testproj\classes helloworld 3, Appclass Loader (System class loader Appclassloader): Loads the path or jar specified by System.getproperty ("Java.class.path"). When using Java to run a program, you can also add-CP to overwrite the original classpath settings, for example: JAVA-CP./lavasoft/classes helloworld  Extclassloader and Appclassloader after the JVM is started, a copy is saved in the JVM, and the search path cannot be changed while the program is running. If you want to load a class from another search path at run time, a new classloader will be generated.


Java typically uses two Path:classpath and Java.library.path

Classpath is the location that points to the jar package

Java.library.path the location of the non-Java class package (DLL,SO)


The system variable Ld_library_path under 1:linux to add Java.library.path

2: Add-djava.library.path=/usr/local/lib in VM arguments

3: See


Development, application will always encounter outofmemory anomalies, and often after a period of time the memory is eaten up, here can use Java Heap dump out of the JVM memory image, and then analyze it to find the problem.

The Java heap dump triggering and analysis article is a good introduction to heap dump methods and tools for analysis. It's easy to use jmap-dump:format=b,file=/path/file.hprof <pid> this Java-brought tool to dump the heap, but when the memory overflow problem occurs faster, The order may be too late or invalid. It is convenient to configure the relevant parameters-xx:+heapdumponoutofmemoryerror at this time when the application starts, and of course you can add-xx:heapdumppath=/path/file.hprof to specify the output path of the file. Do not know how to use these parameters? Add when you start the app, such as:/usr/lib/jvm/java-1.6.0/bin/java-server-xms1536m-xmx1536m-xx:newsize=256m-xx:maxnewsize=256m-xx: Permsize=64m-xx:maxpermsize=64m-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=/path/file.hprof-djava.ext.dirs =/xxx/lib/classname in Java heap dump triggering and analysis, there is an analysis tool, the personal feel that the use of Java comes with the $JAVA _home/bin/jhat-j-xmx512m/path/ FILE.HPROF tools Look at the results of the analysis is not very friendly, of course, the situation is urgent, and there is no tools around the case is a good choice. However, individual comparisons recommend the IBM Heapanalyzer (not used) and Memoryanalyzer described in this article, and the introduction to Memoryanalyzer can be seen in the analysis of heap dump files using Eclipse Memory Analyzer.


If the configuration-classpath trouble, you can use the-djava.ext.dir parameter substitution, so you can bulk introduce jar package.

However, there is a problem to note that JAVA defaults to the Ext directory is $java_home/jre/lib/ext, so if you specify the-djava.ext.dir parameter, the original $java_home/jre/lib/ext under the JAR package will not be referenced, May cause problems with your program;

For example, where the SSL protocol is useful, a Javax.net.ssl.SSLKeyException:RSA premaster secret error exception occurs.
I recently encountered this problem when I joined sqlserver2008 through JDBC in my project because I replaced-classpath with the-djava.ext.dir parameter.

The workaround is to place the Dnsns.jar,localedata.jar,sunjce_provider.jar,sunpkcs11.jar under $java_home/jre/lib/ext under the Java.ext.dir directory you specified .

To prevent inexplicable errors, it is best to copy the jar packages under the entire $java_home/jre/lib/ext.



JVM Common parameter Settings good

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.