I am getting an OutOfMemoryError: Java heap space
Are there any tools I can use to find the root cause ?
You can analyse the heap dump of your application using some analysis tool like eclipse mat to see what's consuming how much of the heap.
But first you need to obtain the heap dump of your application.
To have the JVM automatically generate the heap dump for you when the OOM error occurs you can use the -XX:+HeapDumpOnOutOfMemoryError
option. On top of that, you can also use the -XX:HeapDumpPath
option to tell JVM where to generate the file.
java -XX:HeapDumpPath="D:\heapdumps\YourApp.hprof" -XX:+HeapDumpOnOutOfMemoryError -jar YourApp.jar
Once that file is generated you can open it in mat and do your ananlysis.
You can also manually generate the heap dump at any point while your application is running. For this purpose you can use the jmap
command that comes with jdk.
jmap -dump:live,format=b,file="D:\heapdumps\YourApp.hprof" process_id_of_your_app
You can use the tool - jps
, which also comes with jdk, to easily find the process id of your application.
jps -m
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With