This post originated from an RSS feed registered with Java Buzz
by Scott Delap.
Original Post: Find Leaking SWT Resources with S-Leak
Feed Title: ClientJava.com
Feed URL: http://www.clientjava.com/archives/wireless_mobile.rdf
Feed Description: Client/Desktop Related Java Development
Recently, I had to track down some GDI handle issues with an Eclipse plugin based application. SWT Resources such as Color, Cursor, Display, Font, GC, Image, Printer, Region, Widget and subclasses allocate system resources in their constructors. As a result you must dispose of them when finished. Normally widgets don't cause that much of an issue because disposing a parent widget disposes its children. However, other objects such as images can quickly add up. In the case of my problem, I had written a label provider that created custom images for each node of a tree. Further compounding matters was the fact that numerious intermediate images were created in the process of deriving each final image. After adding a few quick lines of caching code I went from 500+ GDI handles being created to 20. Finally, I was able to add a dispose listener to release the 20 handles when the label provider was no longer needed. During this process I found a useful tool called Sleak. Sleak tracks resources and where they were allocated. There are both a standalone and Eclipse plugin version of Sleak. The 1.x version runs under Eclipse 2.0. I also found an updated version hid in the Eclipse bug tracker for Eclipse 3.x.
SWT: Managing Operating System Resources SWT uses operating system resources to deliver its native graphics and widget functionality. Allocating and freeing operating system resources is traditionally an area of programming that is error prone. Languages that include garbage collection, such as the Java language, relieve the programmer from the burden of managing memory, but not from the allocation and freeing of operating system resources. This article discusses the simple strategy used by SWT to help application designers manage operating system resources.
Sleak 1.x Sleak is a simple tool that monitors SWT graphics resources. (Applications typically don't leak widget resources because of rule 2). You can use Sleak to detect leaks in SWT application code.
The S-Leak Plugin and core, revamped for Eclipse 3.0 For those of you who dont know about S-Leak, it's a dangling resource tracker
for SWT based applications. In SWT when we allocate resources such as fonts or
images, we have to make sure to dispose them later. However, sometimes we miss
them and this guy helps you find the ones you missed. It provides a stack trace
of when the resource was created as well as a preview of the resource (if
possible). It uses A LOT of Eclipse functionality to accomplish this.