There a lot of JVM arguments that affect the JVM\'s memory usage like -Xms, -Xmx, -Xns, -XX:MaxPermSize...
Read JVM options explained. Here're excerpts from it:
java.lang.OutOfMemoryError: Java heap space
errors you should increase -Xmx
value.-XX:PermSize
and -XX:MaxPermSize
do not need to be adjusted but if you are seeing java.lang.OutOfMemoryError: PermGen space
events often you can increase these values.-XX:MaxJavaStackTraceDepth
- This specifies how many entries a stack trace for a thrown error or exception can have before a StackOverflowError
is thrown. So if you are dealing with huge stack traces you can use this option to increase the allowed entriers before overflow.There are hundreds of JVM options available. Basically they are classified into three types:
List of few standard options: [To see complete list execute the command "java" without any option]
-client to select the "client" VM
-server to select the "server" VM
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-version print product version and exit
-showversion print product version and continue
-X print help on non-standard options`
List of few non-standard X options: [To see complete list execute the command "java -X"]
-Xincgc enable incremental garbage collection
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
List of few non-standard XX options: [Complete list available here]
-XX:InitialHeapSize=<size> set initial Java heap size. Same as -Xms<size>.
-XX:MaxHeapSize=<size> set maximum Java heap size. Same as -Xmx<size>.
-XX:+PrintFlagsFinal prints all JVM options passed.
-XX:+UnlockDiagnosticVMOptions opens up lot more VM options.
If you want to enhance your knowledge in JVM options, please refer this blog. The link is just part 1 out of 8. Find out and read other parts as well.
-Xss: Stack size.
Used to set the size of your stack. Stack values only exist within the scope of the function they are created in. Once the function returns, they are discarded.
The easiest way to run out of stack space is to recurse too deep.
-Xms, -Xmx: Min and max heap size.
Used to set the size of your heap. The heap is where you allocate objects. Objects persist until they are garbage collected.
The easiest way to run out of heap space is to allocate something massive.
-XX:MaxPermSize: Permanent generation.
The permanent generation is special because it holds data needed by the virtual machine to describe objects that do not have an equivalence at the Java language level. For example objects describing classes and methods are stored in the permanent generation.
You usually run out of permgen space if you are somehow leaking references to classes you load dynamically. This plagues some web containers in particular.
-Xms:
this option sets the initial and minimum Java heap size.
-Xmx:
This option sets the maximum Java heap size. The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection.
-XX:PermSize:
-XX:MaxPermSize:
are used to set size for Permanent Generation. The permanent space is where are stored the class, methods, internalized strings, and similar objects used by the VM and never deallocated (hence the name).
-Xss:
sets the thread stack size. Thread stacks are memory areas allocated for each Java thread for their internal use. This is where the thread stores its local execution state.
-Xns:
sets the nursery size. the JRockit JVM uses a nursery when the generational garbage collection model is used, that is, when the dynamic garbage collector has determined that the generational garbage collection model should be used or when the static generational concurrent garbage collector ( -Xgc : gencon) has been selected. You can also use -Xns to set a static nursery size when running a dynamic garbage collector (-XgcPrio).
If you are getting java.lang.OutOfMemoryError: Java heap space
than change the value of -Xmx
and -Xms
.
if you are getting java.lang.OutOfMemoryError: PermGen space
than try increasing the - XX:MaxPermSize
value.
if you are getting java.lang.StackOverflowError
than try increasing the -Xss
value. It may be helpful by increasing the stack size but you should have a look at your code as well.