Wednesday, October 31, 2012

My Summary of JVM

Java Virtual Machine is a program which runs java bytecode. Java Byte is a intermediate code for Java programs. JVM runs .class and .jar files.

Better performance of server is possible only through proper configuration of JVM parameters. The two important parameters for performance are Heap and Garbage collection.

What is Heap?

JVM uses Heap Memory for dynamic memory allocation. Heap is divided into generations:
1) The young generation stores (EDEN) short-lived objects that are created and immediately garbage collected.
2)Objects that persist longer are moved to the old generation (also called the tenured generation).
3)The permanent generation (or permgen) is used for class definitions and associated metadata.


-Xms initial java heap size
-Xmx maximum java heap size
-Xmn the size of the heap for the young generation


GC - Garbage Collection:

There are essentially two GC threads running. One is a very lightweight thread which does "little" collections primarily on the Eden (a.k.a. Young) generation of the heap. The other is the Full GC thread which traverses the entire heap when there is not enough memory left to allocate space for objects which get promoted from the Eden to the older generation(s).If there is a memory leak or inadequate heap allocated, eventually the older generation will start to run out of room causing the Full GC thread to run (nearly) continuously. Since this process "stops the world". The amount allocated for the Eden generation is the value specified with -Xmn. The amount allocated for the older generation is the value of -Xmx minus the -Xmn. Generally, you don't want the Eden to be too big or it will take too long for the GC to look through it for space that can be reclaimed.

Basic algorithm of GC: mark-and-sweep

Verbosegc:
Using verbosegc, which is enabled by the -verbosegc option, is a good way to see what's going on with GC. Output is displayed on stderr for every GC cycle, giving information about what occurred. You can use the information to tune the heap size or diagnose any problems. Note that verbosegc can, and usually does, change between releases

Stack size:

Each thread in the VM get's a stack. The stack size will limit the number of threads that you can have, too big of a stack size and you will run out of memory as each thread is allocated more memory than it needs.

-Xss        the stack size for each thread
-Xss determines the size of the stack: -Xss1024k. eventually you will see an exception class java.lang.StackOverflowError .

Monitoring the JVM

JDK 5 includes a number of tools that are useful for monitoring the JVM. Documentation for these tools is available from the Sun website. For JDK's prior to 5, Sun provides the jvmstat tools .

win> ./httpd.exe -Dcom.sun.management.jmxremote
unix> bin/httpd.sh -Dcom.sun.management.jmxremote

-Dcom.saba.util.Debug.SabaError=true will generate Saba Error log where it returns the details of the saba.

JVM Parameters:
-Xgcpolicy:gencon -verbose:gc,sizes -Dcom.saba.util.Debug.SabaError=true
-Xgcpolicy:gencon -Xmns384M -Xmnx1536M -Xmos1152M -Xmox4608M -verbose:gc,sizes -Dcom.saba.util.Debug.SabaError=true



-Xgcpolicy:[optthruput]|[optavgpause]|[gencon]|[metronome]
Controls the behavior of the Garbage Collector.
  • The optthruput option is the default and delivers very high throughput to applications, but at the cost of occasional pauses.
  • The optavgpause option reduces the time that is spent in these garbage collection pauses and limits the effect of increasing heap size on the length of the garbage collection pause. Use optavgpause if your configuration has a very large heap.
  • The gencon option requests the combined use of concurrent and generational GC to help minimize the time that is spent in any garbage collection pause


The -Xgcpolicy:optavgpause command-line option requests the use of concurrent garbage collection. Concurrent GC reduces the pause time by performing some garbage collection activities concurrently with normal program execution without causing disruption.During concurrent garbage collection, a significant amount of time is wasted identifying relatively long-lasting objects that cannot then be collected.

Generational GC reduces pause times by dividing the heap into two generations: the "new" and the "tenure" areas. Objects are first allocated to the new area; if they have active references for long enough, they are promoted to the tenure area.  Generational GC reduces pause times by concentrating the effort to reclaim storage on the new area because it has the most recyclable space. New area is small enough, pause times are comparatively short. 

 The -Xgcpolicy:gencon option requests the combined use of concurrent and generational GC to help minimize the time that is spent in any garbage collection pause.



-Xmn<size>
Sets the initial and maximum size of the new (nursery) heap to the specified value when using -Xgcpolicy:gencon. Equivalent to setting both -Xmns and -Xmnx. If you set either -Xmns or -Xmnx, you cannot set -Xmn. If you attempt to set -Xmn with either -Xmns or -Xmnx, the VM will not start, returning an error. By default, -Xmn is selected internally according to your system's capability. You can use the -verbose:sizes option to find out the values that the VM is currently using.
-Xmns<size>
Sets the initial size of the new (nursery) heap to the specified value when using -Xgcpolicy:gencon. By default, this option is selected internally according to your system's capability. This option will return an error if you try to use it with -Xmn.
-Xmnx<size>
Sets the maximum size of the new (nursery) heap to the specified value when using -Xgcpolicy:gencon. By default, this option is selected internally according to your system's capability. This option will return an error if you try to use it with -Xmn.
-Xmo<size>
Sets the initial and maximum size of the old (tenured) heap to the specified value when using -Xgcpolicy:gencon. Equivalent to setting both -Xmos and -Xmox. If you set either -Xmos or -Xmox, you cannot set -Xmo. If you attempt to set -Xmo with either -Xmos or -Xmox, the VM will not start, returning an error. By default, -Xmo is selected internally according to your system's capability. You can use the -verbose:sizes option to find out the values that the VM is currently using.
-Xmos<size>
Sets the initial size of the old (tenure) heap to the specified value when using -Xgcpolicy:gencon. By default, this option is selected internally according to your system's capability. This option will return an error if you try to use it with -Xmo.
-Xmox<size>
Sets the maximum size of the old (tenure) heap to the specified value when using -Xgcpolicy:gencon. By default, this option is selected internally according to your system's capability. This option will return an error if you try to use it with -Xmo.



Adjustments made for Tuning:





No comments:

Post a Comment