Searching for Bukkit Memory Leaks
In this post I will describe how to search for memory leaks in your Bukkit Minecraft SMP server.
Getting a heap dump
The first step is to obtain a Java heap dump to inspect. This is a nice article on the subject: http://blog.emptyway.com/2007/04/02/finding-memory-leaks-in-java-apps/
That article describes some different ways to obtain a heap dump. If you are using a JDK version of Java instead of an JRE, you have some neat tools at your disposal 🙂. For example hprof and jmap.
However none of those tools work well together with Bukkit. The tool
jmap hangs the Minecraft server and then crashes it. The tool
hprof makes the server slow down to a crawl.
So we are going to use another method. We will instruct Java to dump the heap if the application (Bukkit) runs out of memory. We do this by passing some additional arguments when starting the server:
Your command could now look like this:
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp -Xms3G -Xmx3G -jar craftbukkit-0.0.1-SNAPSHOT.jar
Now you should run your server till it crashes. The out of memory errors on my server looks something like this:
java.lang.OutOfMemoryError: GC overhead limit exceeded
And after the crash I have a file named
/tmp/java_pid1577.hprof. This is the heap dump we want to analyze. In order to do that I transfer it to my desktop computer using FileZilla. You would might want to compress the file before you transfer it. My file was 2.5 gigabytes so I compressed it to 186 megabytes before transferring it (6% compressions ratio 😮)
gzip -9 java_pid1577.hprof
Analyzing the heap dump
You can now download this heap dump to your own computer and analyze it. Here are some nice free tools for doing just that: