Friday, March 21, 2008

Plugging SWT Leaks in Your Eclipse App

During a recent bout of looking for leaks in my Eclipse Application, I ran into a limitation of my tool of choice, JProfiler, or at least, a limitation of my understanding of it. One problem I was having was the "No more Handles" error when repeatedly opening my editor. After some digging, I found a tool written by someone in the SWT team: Sleak. The "limitation" of JProfiler is that it works more from a generic Java memory point of view. I needed to find where handles were leaking - even if they were not using that much memory. Sleak solves this simply.

First, I'll go through getting it set up - it didn't work exactly for me as in the included instructions - but you'll need to refer to them to understand what I'm talking about from here. Just dropping it into my plugins dir as directed didn't work (not blaming the instructions here - this could've been my fault). I ended up with Class loader errors. I decided to repackage it myself, so I started by importing it into my workspace as an Eclipse project. Then, I exported it as a jar from the plugin overview screen. At this point, I had a Sleak jar in the specified output directory. I dropped this into my plugins directory, and followed the other instructions. The only other change I would recommend here is to just set the properties to true instead of adding lines since they are already in the .options file.

Now you're ready to find leaks. Launch Eclipse normally with the workspace containing all the code for your application. Open your favorite debug configuration and add "-debug -clean" to the end of your Program Arguments. Then, just launch the configuration. Once it loads, open the Sleak view by using the Window->Show View->Other->SWT Tools menu. The buttons you'll see are a little different from the instructions - you will see "Snap" and "Diff" which I interpret as "Take a Snapshot of the Handles in Use" and "Take Another One and Show the Differences from the Previous Snapshot". All you have to do is use the Snap button, preform the operation you think has leaks and then press Diff. You will get a nice list of the handles still hanging around. Clicking on them will often show you a preview in the window (this works for Colors, Images, and a few other visual handles). Clicking the stack box will even show you were in the code they were created.

I hope this helps. I really appreciate the work by the SWT team for releasing this tool for all us developers! If you wrote this, please leave a comment so you can take some credit.

No comments: