Time for action – finding the leak
It is necessary to know how many resources are allocated in order to know whether the leak has been plugged or not. Fortunately, SWT provides a mechanism to do this via the Display
and the DeviceData
class. Normally, this is done by a separate plug-in, but in this example, the ClockView
will be modified to show this behavior.
- At the start of the
ClockView
class'screatePartControl
method, add a call to obtain the number of allocated objects, via theDeviceData
of theDisplay
class:public void createPartControl(Composite parent) { Object[] objects = parent.getDisplay().getDeviceData().objects;
- Iterate through the allocated
objects
, counting how many are instances ofColor
:int count = 0; for (int i = 0; i < objects.length; i++) { if (objects[i] instanceof Color) { count++; } }
- Print the
count
to the standard error stream:System.err.println("There are " + count + " Color instances");
- Now run the code in debug mode and show the Clock View. The following lines will be displayed in the host Eclipse Console View:
There are 0 Color instances There are 0 Color instances There are 0 Color instances
Note
For efficiency, SWT doesn't log all the allocated resources all the time. Instead, it's an option that is enabled at startup through an
options
file, which is a text properties file withname=value
pairs. This can be passed to an Eclipse instance at launch via the-debug
flag. Fortunately, it is easy to set within Eclipse from the launch configuration's tracing tab. - Close the target Eclipse application, if it is running.
- Go to the launch configuration via the Debug | Debug Configurations menu.
- Select the Eclipse Application (if it's not selected already) and go to the Tracing tab. Enable the tracing option, and select the
org.eclipse.ui
plug-in. Select both the debug (at the top) and the trace/graphics options: - Now launch the application by hitting Debug, and open and close the
ClockView
a few times:There are 87 Color instances There are 92 Color instances There are 95 Color instances There are 98 Color instances
What just happened?
Clearly, something is leaking three Color
instances each time the Clock View is opened. Not surprisingly, three instances of the Color
are allocated in the three instances of ClockWidget
. This suggests that there is a resource leak in the ClockView
or ClockWidget
.
When SWT is running in trace mode, it will keep a list of previously allocated resources in a global list, which is accessible through the DeviceData
object. When the resource is disposed, it will be removed from the allocated list. This allows monitoring of the state of resources at play in the Eclipse workbench and discovering leaks, typically through repeated actions, noting an increase each time in the resource count.
Other object types are also stored in this list (for example, Font
and Image
instances), so it's important to filter by type when looking for a resource set. It's also important to note that Eclipse has its own runtime resources which are used, and so during tracing, these are included in the list as well.
By learning how to enable tracing and how to programmatically detect what objects are allocated, it will be possible to discover such leaks or check whether they have been fixed afterwards.