This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 398381 - [Contributions] Memory leak when ever calling context menu
Summary: [Contributions] Memory leak when ever calling context menu
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.3.2   Edit
Assignee: Platform UI Triaged CLA
QA Contact: Paul Webster CLA
URL:
Whiteboard:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2013-01-17 07:15 EST by Snjezana Peco CLA
Modified: 2014-01-30 12:36 EST (History)
4 users (show)

See Also:


Attachments
A patch (11.50 KB, patch)
2013-01-17 07:18 EST, Snjezana Peco CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Snjezana Peco CLA 2013-01-17 07:15:19 EST
Steps to reproduce:
Call a context menu in a view/editor more times

The memory leak happens when calling a context menu in the Package Explorer view, the Java editor, the Outline view ...
Many contribution items and elements are leaked.
The issue happens for the following two reasons:

- the PopupMenuExtender class seems to have to call the unlink method when the context menu is hidden.
- the MenuManagerRendere class contains the sharedElementToRecord hash map that uses MMenuElement as a key. The classes that implement the MMenuElement interface don't override hashCode (and equals), but the ContributionRecord.mergeIntoModel method clones those elements that are contributed using the 'popup:org.eclipse.ui.popup.any' uri. That's why the sharedElementToRecord hash map contains leaked elements.

I have solved the issue so that the PopupMenuExtender remembers such entries and clears them when the menu is hidden.

Attached is a patch.
Comment 1 Snjezana Peco CLA 2013-01-17 07:18:53 EST
Created attachment 225753 [details]
A patch
Comment 2 Paul Webster CLA 2013-09-25 15:39:17 EDT
The mergeIntoModel discard the clone if it can find an already exiting model element:

shared.setVisibleWhen(merge(copy.getVisibleWhen(),
			shared.getVisibleWhen()));
copy = shared;

The existing model element should be used then, right?

PW
Comment 3 Snjezana Peco CLA 2013-09-30 10:45:15 EDT
I suppose it should work that way.
But it seems there are elements that don't have equals/hashCode (or it isn't based on the elementId property).
Because of that the line:

MMenu shared = findExistingMenu(copy.getElementId())

can't always find the existing menu and those elements aren't removed from the sharedElementToRecord hash map.
Comment 4 Paul Webster CLA 2013-11-21 14:31:46 EST
Can we not switch getList(item) to addRecord/removeRecord?

Then when the last ContributionRecord is removed from association from a specific item, we can do a sharedElementToRecord.remove(item) and the keys won't be leaked any more.

PW
Comment 5 Paul Webster CLA 2013-11-21 14:44:21 EST
Switching to an add/remove for the sharedElementToRecord does retain some elements, but at a stable number (opening/closing the same context menu over and over doesn't cause the number of keys to continue to grow).

Was this map the main concern of leaked objects?  Or were there others?

PW
Comment 6 Paul Webster CLA 2013-11-21 15:01:53 EST
Suggested fix with add/remove https://git.eclipse.org/r/#/c/18692/

PW
Comment 7 Dani Megert CLA 2013-11-22 05:40:17 EST
(In reply to Paul Webster from comment #6)
> Suggested fix with add/remove https://git.eclipse.org/r/#/c/18692/
> 
> PW

Can we first test drive this on 'master'?
Comment 8 Paul Webster CLA 2013-11-22 05:52:20 EST
(In reply to Dani Megert from comment #7)
> 
> Can we first test drive this on 'master'?

Yes, I agree.

PW
Comment 9 Paul Webster CLA 2013-11-22 11:03:11 EST
Released a fix in master to testdrive the fix for a while: http://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/?id=9c42f70f507a5906e6a9faf634b1951fee9f7509

PW
Comment 10 Paul Webster CLA 2013-12-19 09:57:41 EST
(In reply to Paul Webster from comment #6)
> Suggested fix with add/remove https://git.eclipse.org/r/#/c/18692/

Paul, could you please review this?

PW
Comment 12 Paul Elder CLA 2014-01-30 12:36:39 EST
Verified in 4.3.2.M20140129-0800