This is an old revision of the document!


Java (jvm) tips and tricks

Enviroment settings

Linux (with multiple java enviroments)

.bashrc / bash.profile

 export JAVA_HOME="/usr/local/java/"
 export PATH=/usr/local/java/bin:$PATH

Tunning

switch description
-Xmx maximum java heap size - defines the max memory size that the heap can reach for the JVM. You must know your program well and see how it performs under load and set this parameter accordingly. A low value can cause OutOfMemoryExceptions or a very poor performance if your program's heap memory is reaching the maximum heap size. If your program is running in dedicated server you can set this parameter higher because it wont affect other programs
-Xms initial java heap size - sets the initial heap memory size for the JVM. This means that when you start your program the JVM will allocate this amount of memory instantly. This is useful if your program will consume a large amount of heap memory right from the start. This avoids the JVM to be constantly increasing the heap and can gain some performance there. If you don't know if this parameter is going to help you, don't use it.
-Xmn the size of the heap for the young generation -Young generation represents all the objects which have a short life of time. Young generation objects are in a specific location into the heap, where the garbage collector will pass often. All new objects are created into the young generation region (called “eden”). When an object survive is still “alive” after more than 2-3 gc cleaning, then it will be swap has an “old generation” : they are “survivor” . Good size is 33%
-Xss the stack size for each thread
-XX:NewRatio the same as Wmn, but using a % (dynamic fs static -Xmn option) -XX:NewRatio=3 means that the ratio between the old and young generation is 1:3
-XX:NewSize Size of the young generation at JVM init. Calculated automatically if you specify -XX:NewRatio
-XX:MaxNewSize The largest size the young generation can grow to (unlimited if this v alue is not specified at command line)
-XX:SurvivorRatio “old generation” called tenured generation, ratio, in %. For example, -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden to be 1:6 (eden is where new objects are created)
-XX:MinHeapFreeRatio default is 40%. JVM will allocate memory to always have as minimum 40% of free memory. When -Xmx = -Xms, it's useless.
-XX:MaxHeapFreeRatio: default is 70%. The same as Min, to avoid unecessary memory allocation.
  • It is good practice with server-side Java applications like Resin to set the minimum -Xms and maximum -Xmx heap sizes to the same value.
  • For efficient garbage collection, the -Xmn value should be lower than the -Xmx value.

Good/best practices

Examples

Troubleshooting

Resolving java.lang.OutOfMemoryError: PermGen

if you get an error like:

 java.lang.OutOfMemoryError: PermGen

The PermGen space is used for things that do not change (or change often). e.g. Java classes. So often large, complex apps will need lots of PermGen space. Similarly if you are doing frequent war/ear/jar deployments to running servers like Tomcat or JBoss you may need to issue a server restart after a few deploys or increase your PermGen space.

To increase the PermGen space use something like: -XX:MaxPermSize=128m (The default is 64MB.)

(Note that Xmx is separate from the PermGen space, so increasing Xmx will not help with the PermGen errors).
The PermGen memory in addition to the Xmx memory. e.g. -Xmx128m and MaxPermSize=128m would use up to 256MB.

java.1354661827.txt.gz · Last modified: 2012/12/04 23:57 by a
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready