This article will detail what the java heap is and how to change java heap size. If you continually run out of heap memory you should consider increasing the amount of heap size your jvm (Java Virtual Machine) is utilizing.
A heap dump contains all of the objects that are in use in the JVM. In the event you ran out of heap memory, if you dump the heap at that point it will contain the objects in use when the JVM encountered the out of memory error. The heap is created when the JVM is started and is periodically cleaned by garbage collection.
Change Java Heap Size
Consider the following JVM options when changing the java heap size.
| Option | Description |
| -Xms | Initial java heap size |
| -Xmx | Maximum java heap size |
Note that the initial java heap size does not have to be the same as the maximum heap size. If less, the JVM will startup more quickly and dynamically grow as needed.
Note that heap size does not affect how much memory your java process uses, so it needs to be set accordingly. Pay attention to the amount of memory (RAM) your physical or virtual server uses so that you don’t increase the JVM heap size more than the server can handle.
For example, to change the initial and maximum java heap size in your java process, run the following command. This example sets the initial and max heap size to 2048M or 2GB.
java -Xms2048M -Xmx2048M -jar example.jarJava Heap Dump on Out of Memory
The java.lang.OutOfMemoryError will crash your java process. Depending on your stack, you should implement safeguards to restart the process if it runs out of memory.
To dump your java heap on an out of memory error, add the following options to your java process startup.
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump.hprofTo perform a manual heap dump, use the jps utility.
- Get the java process PID. Run
jps -v - Get the heap dump. Run
jmap -dump:live,format=b,file=dump.hprof
Java Metaspace vs Permgen
Starting with Java 8, Metaspace replaces PermGen. Metaspace memory space grows, whereas permgen was fixed.
The java.lang.OutOfMemoryError: Metaspace error indicates that while your heap space is fine, the metaspace is out of memory. The metaspace is memory allocated for Java class metadata. This too will cause your java process to hang.
To set the max metaspace size, add this option to your java startup command.
-XX:MaxMetaspaceSize=<metaspace size>[unit]To set the metaspace size, add this option to your java startup command.
-XX:MetaspaceSize=<metaspace size>[unit]The metaspace size does not have to be the same as the max. It will dynamically grow if set to less.
If you have a heap dump handy after falling to the OutOfMemoryError: Metaspace, then you can inspect the dump and look for duplicate classes.
Monitor Metaspace
You can use the jstat utility to monitor metaspace capacity.
First, get the java process id (PID).
jps -vNext, run this jstat command to get the metaspace capacity in kb.
jstat -gcmetacapacity (PID)Finally, compare the result with what you set for the maxmetaspacesize and metaspacesize in previous examples using -XX:MaxMetaspaceSize=<metaspace size>[unit] -XX:MetaspaceSize=<metaspace size>[unit]
Conclusion – Change Java Heap Size
This article has demonstrated how to change java heap size, dump your java heap, and inspect the heap for out of memory causes and metaspace error causes. Leave us a comment if you have questions or would like to see more examples on troubleshooting java heap errors.
Leave a Reply