It is important that before we do any type of JVM tuning, we should throughly profile our system.
Memory leaks caused by our application code cannot be fixed by jvm tuning.
Java memory is divided into two main parts:
1. Heap size = (Young Gen, Tenured Gen,Permanent Gen)
2. Stack Size allocated to each thread.
Total Memory Used = heap + (stack size)*number of threads
JVM memory settings.
These settings can be tweaked to avoid the java.lang.OutOfMemoryError.
-Xms Set minimum available memory.
-Xmx Set maximum available memory.
-Xmn Set size of young generation.
-Xss Set the size of stack.
Where is the memory size in k,m etc.
java -Xmx1024m -Xms512m -Xmn256m -Xss1024k HelloWorld
This example sets the maximum available memory to 1024 MB, Start Memory to 512 MB, New Generation 256 MB and Stack Size to 1024 KB.
For more options run the following command at the command line.
$ java -X
-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
-Xbootclasspath: set search path for bootstrap classes and resources
-Xbootclasspath/a: append to end of bootstrap class path
-Xbootclasspath/p: prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection. Use with care.
-Xincgc enable incremental garbage collection
-Xloggc: log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms set initial Java heap size
-Xmx set maximum Java heap size
-Xss set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs reduce use of OS signals by Java/VM (see documentation)
-Xcheck:jni perform additional checks for JNI functions
-Xshare:off do not attempt to use shared class data
-Xshare:auto use shared class data if possible (default)
-Xshare:on require using shared class data, otherwise fail.
For the full list of JVM options refer http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html