Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools for OutOfMemoryError java heap space analysis

I am getting an OutOfMemoryError: Java heap space

Are there any tools I can use to find the root cause ?

like image 759
l0r3nz4cc10 Avatar asked Dec 26 '22 11:12

l0r3nz4cc10


1 Answers

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
like image 182
Bhesh Gurung Avatar answered Jan 07 '23 17:01

Bhesh Gurung