Posts Tagged ‘visualvm’

Using WeakReference to confirm/find memory leaks in Java

Friday, September 4th, 2009

I recently had a memory leak in my Trekwar game, and to find it (or at least confirm it’s there) I used the WeakReference object in Java..

This is how I proceeded in locating the memory leak, this method could easily be adapted to other programs.

Trekwar is turn based, so each turn the client downloads a Galaxy object from the server. There are many references to this objects inside action listeners, threads, etc..

WeakReference
To make a long story short, a weak reference is like a normal reference, except it will not prevent the object it points to from being garbage collected.

This means that is you  have a normal and a weak reference to object A, the weak reference will be null if you remove the “hard” reference.

1) In the client main executable (the class that holds the method that updates the map from the server). Import, declare and make a list of weak references

import java.lang.ref.WeakReference;
private ArrayList<WeakReference<Galaxy>> weakRefs;
 
weakRefs = new ArrayList<WeakReference<Galaxy>>();

2) Find the place in your program where a new object is being added, in my place this is the localGalaxy object. Create a weak reference to this object, and list all your weak references.

localGalaxy = (Galaxy) objStream.readObject();
weakRefs.add(new WeakReference(localGalaxy));
for(int i = 0; i < weakRefs.size(); i++) {
System.out.print("ref # " + i + " = ");
try {
Galaxy testG = (Galaxy)weakRefs.get(i).get();
System.out.println(testG + ", tick = " + testG.currentTick);
}
catch(NullPointerException npe) {
System.out.println("null");
}
}

And you’re done!

Now when you run the program, you should see something like this each time the method is called

ref # 0 = org.aakretech.trekwar2.common.Galaxy@11ed166, tick = 37
ref # 1 = org.aakretech.trekwar2.common.Galaxy@9801f4, tick = 37
ref # 2 = org.aakretech.trekwar2.common.Galaxy@1c5d81c, tick = 38
ref # 3 = org.aakretech.trekwar2.common.Galaxy@101f8f4, tick = 39
ref # 4 = org.aakretech.trekwar2.common.Galaxy@1ed1e7e, tick = 40
ref # 5 = org.aakretech.trekwar2.common.Galaxy@e0a7ea, tick = 41

Unless you want to wait until your GC decides it’s time to go, use a tool like VisualVM to force the Garbage Collector to run.

After the GC has run, the output should look like this:

ref # 0 = null
ref # 1 = null
ref # 2 = null
ref # 3 = null
ref # 4 = null
ref # 5 = null
ref # 6 = org.aakretech.trekwar2.common.Galaxy@2b3c91, tick = 42

Now, this means that the object IS being garbage collected, and there is no memory leak.. If you run this and the object you are monitoring (in this example the localGalaxy object) is not GC’ed because of stray references, you can tell because none of the references will point to null.

VisualVM – nifty java tool

Wednesday, July 9th, 2008

I downloaded a new Java tool today, called VisualVM which was released (version 1.0) yesterday and is going to ship with the SUN JDK starting with JDK 6 release 7.

This is a tool that allows you to view information about the java programs currently running on your computer (or a remote computer). It does not seem to provide as much information as JConsole or JManage, but it still gives the most essential information. Besides the tool is still pretty new (version 1.0), so I’m sure we will see lots of improvement in the near future.

Monitoring provides basic information about uptime, memory usage, threads and some other stuff:
Visual VM Monitoring

The application also has a rudimentary profiler which allows you to see which methods are using cpu cycles, and which objects are hogging all the memory,
VisualVM profiler

The program can also do a heap dump, allowing you to see the size and number of instances of each class. You can also inspect each and every object to view it’s variables and references:
VisualVM heap dump

The profiling part was pretty simple and would benefit from more work, but overall I’m pretty sure this tool(even though a bit low on features/information in it’s current state)  will be a big hit, and a good entry to profiling and application management to many people.

A very nice feature I did not take a screenshot of is a visual timeline view of all the threads and which state they are in, very smooth :)   The software is also suppose to let you view core dumps, but did not get a chance to test this feature tonight.