Bug 431928 - Memory leak in FormImages#markFinished method
Summary: Memory leak in FormImages#markFinished method
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: User Assistance (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.5 M6   Edit
Assignee: Tomasz Zarna CLA
QA Contact:
URL:
Whiteboard:
Keywords: bugday, greatfix, helpwanted
Depends on:
Blocks:
 
Reported: 2014-04-03 11:07 EDT by Peter Severin CLA
Modified: 2015-03-18 19:37 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Severin CLA 2014-04-03 11:07:21 EDT
I did some profiling of my RCP app and I found a memory leak in FormImages#markFinished method. Here's the code in question:

	public synchronized boolean markFinished(Image image, Display display) {
		checkHashMaps();
		AbstractImageDescriptor desc = (AbstractImageDescriptor)descriptors.get(image);
		if (desc != null) {
			LocalResourceManager resourceManager = manager.getResourceManager(display);
			resourceManager.destroyImage(desc);
			if (resourceManager.find(desc) == null) {
				descriptors.remove(image);
				validateHashMaps();
			}
			return true;
		}
		// if the image was not found, dispose of it for the caller
		image.dispose();
		return false;
	}

The problem is with this line:
  descriptors.remove(image);

This line does not actually remove the entry from the descriptors HashMap. This is because the hashCode of the Image object changes (it becomes 0) when the image is disposed and the Image is always disposed in this line. This means that image descriptors area slowly leaked and the descriptors HashMap grows in size. I guess the good thing is that the Image resource is disposed so no graphical resources are leaked, but there is a memory leak nonetheless.

A possible solution to this problem is to use "new Integer(image.hashCode())" as keys in descriptors map. Then the key could be constructed before calling resourceManager.destroyImage(desc) method so that we obtain the hashCode before the image is disposed.
Comment 1 Tomasz Zarna CLA 2015-02-17 17:25:36 EST
https://git.eclipse.org/r/#/c/42077/
Comment 3 Wojciech Sudol CLA 2015-03-03 18:28:02 EST
Thanks Tomasz!