Versions
- Java Oracle JDK 1.6.X
Heap
On program startup, Java allocates a certain amount of memory for the heap. This memory is used to fulfill requests for memory allocation by the program or libraries it calls. The Java heap space also has an upper limit that if reached, results in the java.lang.OutOfMemoryError[3] being thrown. Generally once running, there is no action a program can take to resolve this error. Instead, either set the maximum heap space higher when running the program or use a Java memory profiler to analyze memory usage (there might be a memory leak). It is also possible to redesign the algorithm to use the disk as a temporary swap space, but this should be avoided unless absolutely necessary.
Java allows setting the minimum and maximum heap settings as follows:[1]
- -Xms$MIN_VALUE
- -Xmx$MAX_VALUE
Values are specified as multiples of 1024 bytes and must be greater than 1 MB. Append k for kilobytes, m for megabytes or g for gigabytes.
Examples:
- -Xms16k
- -Xmx32m
- -Xmx2g
If these values are not specified at program startup, Java will automatically determine the values to scale with the available system memory.[2]
PermGen
Java uses a separate pre-allocated memory region, called the PermGen, to store class files, string constants and other resources located in jar files. With the exception of very large programs, the default value for PermGen is sufficient. Issues with PermGen are more common when running an application server such as GlassFish or Tomcat that loads jar files on-demand. To fix issues with PermGen, simply raise the default value for the PermGen space. Note that it is possible to create a classloader leak[5] with on-demand jar file loading.
- -XX:MaxPermSize
Examples:
- -XX:MaxPermSize=128m
Application Server Tuning
It is recommended to run application servers such as GlassFish or Tomcat with the following two settings to optimize garbage collection of unused jar resources in PermGen when an application is undeployed.[7]
- -XX:+CMSClassUnloadingEnabled
- -XX:+CMSPermGenSweepingEnabled
Sources
- http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html
- http://docs.oracle.com/javase/6/docs/technotes/guides/vm/gc-ergonomics.html
- http://docs.oracle.com/javase/6/docs/api/java/lang/OutOfMemoryError.html
- http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap
- http://frankkieviet.blogspot.ca/2006/10/classloader-leaks-dreaded-permgen-space.html
- http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
- http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error