VisualVM slow with heap dump files
One great feature of VisualVM is that it can read heap dump files. Heap dumps are useful to diangose memory leaks. See this post for more details about memory leaks and how to solve them. Another great feature of VisualVM is that you can read a huge heap dump file and VisualVm will consume a minimal amount of memory to do so. For instance, you will be able to read a 8 Gigabytes heap dump file with VisualVM running on a development workstation having only 2 Gigabytes of RAM. In order to achieve that, VisualVM will parse the heap dump file and will create a work file on disk in the default system temp folder (/tmp by default on Linux). In theory that's great, but in practice, VisualVM becomes painfully slow because it constantly have to do disk I/O's to process the information.
This behavior is even more frustrating if you happen to have a server with 12 Gigabytes of RAM available for you. A simple solution for that is to create a ramdisk and tell VisualVM to use that ramdisk as the tmp folder.
First, create the ramdisk. Here I am on a linux development server and I create a tmp folder in my home. Then I create (mount) the ramdisk in the tmp folder I just created:
jdemers@debian12:~$ mkdir /home/jdemers/tmp
jdemers@debian12:~$ sudo mount -t tmpfs none /home/jdemers/tmp
Then I launch VisualVM and I
modify the java.io.tmpdir VM arg that tells VisualVM where the system
tmp folder is.
jdemers@debian12:~$ ./jdk1.6.0_20/bin/jvisualvm -J-Xmx1g -J-Djava.io.tmpdir=/home/jdemers/tmp
Now VisualVM is much much faster and I can investigate and find the root cause of that memory leak much faster.
Solve java.lang.OutOfMemoryError: Java heap space
An OOM or OOME (OutOfMemoryError) simply means that the JVM ran out of memory. When this occurs, you basically have 2 choices:
- Allow the JVM to use more memory using the -Xmx VM argument. For instance, to allow the JVM to use 1 GB (1024 MB) of memory:
java -Xmx1024m ...
- Improve/Fix the application so that it uses less memory
In many cases, like in the case of a memory leak, the second option is the only sound choice. A memory leak happens when the application keeps more and more references to objects and never releases them. The garbage collector will therefore never collect those objects and less and less free memory will be available until we reach the point where not enough free memory is available for the application to function normally. At this point, the JVM will throw an OOM.
A memory leak can be very latent. For instance, the application might behave flawlessly during development and QA. However, it suddenly throws a OOM after several days in production at customer site. To solve that issue, you first need to find the root cause of it. The root cause can be very hard to find in development if it cannot be reproduced in-house. Here are the steps to follow in order to find the root cause and fix that issue:
- Start the application with the VM argument -XX:+HeapDumpOnOutOfMemoryError. This will tell the VM to produce a heap dump when a OOM occurs:
java -XX:+HeapDumpOnOutOfMemoryError ...
- Reproduce the problem. Well, if you cannot reproduce in dev, you will have to use the production environment.
- Use VisualVM to read the heap dump file and diagnose the issue.
First of all, a heap dump is the dump of the heap (duh!). It will allow you to navigate the heap and see what objects use all the heap memory and which are the ones that still keep a reference on them, and so on and so forth. This will give you very strong hints and you will (hopefully) be able to find the root cause of the problem. The problem could be a cache that grows indefinitely, a list that keeps collecting business-specific data in memory, a huge request that tries to load almost all data from database in memory, etc.
Once you know the root cause of the problem, you can elaborate solutions to fix it. In case of a cache that grows indefinitely, a good solution could be to set a reasonable limit to that cache. In case of a query that tries to load almost all data from database in memory, you may have to change the way you manipulate data; you could even have to change the behavior of some functionalities of the application.
If you do not want to wait for a OOM or if you just want to see what is in memory, you can still generate heap dump. To manually trigger a heap dump, you have 2 choices:
- Use VisualVM, right-click on the process on the left pane and select Heap Dump
- If you do not have a graphical environment and can't use vnc (VisualVM needs a graphical environment), use jps and jmap to generate the heap dump file. Then copy the file to your workstation and use VisualVM to read the heap dump (File -> Load...):
Here is what VisualVM looks with a heap dump:user@host:~$ jps
20198
21734 WordFinder
21921 Jps
21168 Main
user@host:~$ jmap -dump:live,format=b,file=heap.bin 21734
Dumping heap to /home/user/heap.bin ...
Heap dump file created
Alternatively, you can also use jhat to read heap dumps.
kill -3 is your friend
One nice feature of Java runtime is when you send the QUIT signal to a Java process, it outputs the full thread dump to stdout. To send the that signal, just open a terminal and type:
kill -QUIT <pid>
or
kill -3 <pid>
Where <pid> is the process Id. This does not terminate the process; all threads will continue doing what they were doing.
That feature can be very useful when the application seems to freeze or when you have a very intermittent issue (intermittent deadlock). With the full thread dump, you can see what every thread was doing at that particular moment. So in case of a deadlock, you will be able to see what monitors and what threads are involved.
This can also be helpful to diagnose performance bottlenecks. Suppose you are load testing an application and it does not deliver the expected throughput, but the CPU usage is not the problem. For instance, with kill -3 you will notice right away that the size of the jdbc connection pool is not big enough and all threads are waiting on it for a connection to free.
Remote Debugging in Java
One thing people ask me from time to time is how to debug a remote Java application. This can be very useful when you experience problems at customer site, but cannot reproduce them in a development environment. We all know logs files do not always contain all information required to solve the issues. In such case, remote debugging can be very useful.
To start remote debugging, one simply needs to add extra VM arguments to the Java command line:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
Then, you can use the remote feature of your IDE to connect to the Java process in using the port specified above (8000). For Instance, in eclipse, you would do:
Run -> Debug Configurations... -> Remote Java Applications -> Create new
In the Host field, you enter the host name or IP of the machine running the Java application. In the Port field, you enter the port specified above (8000).
Note that remote debugging also works nicely with SSH tunnels.