Bug 389335 - CommandContributionItem IBindingManagerListener leak
Summary: CommandContributionItem IBindingManagerListener leak
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.2   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 4.2.2   Edit
Assignee: Oleg Besedin CLA
QA Contact:
URL:
Whiteboard:
Keywords: performance
: 377119 (view as bug list)
Depends on:
Blocks: 385272
  Show dependency tree
 
Reported: 2012-09-11 15:53 EDT by Karen Butzke CLA
Modified: 2013-01-17 14:03 EST (History)
8 users (show)

See Also:


Attachments
Possible patch (5.32 KB, patch)
2012-09-18 11:23 EDT, Oleg Besedin CLA
no flags Details | Diff
Additional patch for "Show view" (2.25 KB, patch)
2012-09-20 11:43 EDT, Oleg Besedin CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Karen Butzke CLA 2012-09-11 15:53:28 EDT
I am using the Git Staging View to see this problem.

I have one unstaged file in my repository.
Right-click on the changed file to view the pop-up menu.
In debug I have a breakpoint in BindingManager.addBindingManagerListener(IBindingManagerListener).

2 of those listeners are added for 2 different CommandContributionItems every time I view that pop-up menu. Those listeners are never removed. The listeners are notified when an editor is opened, closed, and many other window actions. You can see that with a breakpoint in BindingManager.fireBindingManagerChanged(BindingManagerEvent). The list of listeners just keeps growing. Closing the view or perspective does not remove the listeners.

This looks like one major reason that my performance degrades over time (bug 385272). Sorry if this is a duplicate, it seems different than the recent ones Dani has entered (bug 389250 bug 389257).
Comment 1 Karen Butzke CLA 2012-09-11 17:10:21 EDT
My JVisualVM snapshots posted in bug 385272 show this problem, but they don't include profiling of classes in org.eclipse.jface.*. Thus you can't search for the BindingManager classes. But you can still see that CommandContributionItem.update() is called about 100,000 times when closing 30 editors. update() is the method called by IBindingManagerListener.bindingManagerChanged
Comment 2 Nobody - feel free to take it CLA 2012-09-11 17:33:20 EDT
While at it I made a full platform code search for addBindingManagerListener and removeBindingManagerListener. The delta shows that there is a call to add with no apparent removal and is made in org.eclipse.ui.internal.Workbench.initializeDefaultServices()
I am not familiar with the code here and suspect it is because it is some init code but still I think some clean up may be needed as it is done for other listeners registered in the same method.
Comment 3 Ed Willink CLA 2012-09-12 01:16:04 EDT
Failing to remove a listener is a very easy programming mistake that seems to be resolveable only by diligent code review or platform profiling.

With many Eclipse projects in the Simultaneous release and some of them in Incubation, it seems inevitable that many will duplicate this bug. Third party plugins will make the situation worse.

As a result of NPEs, I have reported and queried unexpected command/menu listening against at least EMF Facet and Papyrus.

Is there any way for the platform to protect itself against this? Perhaps an easy to access dump of all the non-platform imposed drains on platform resources and a SimRel requirement for projects to list their expected drains.
Comment 4 Oleg Besedin CLA 2012-09-18 11:23:03 EDT
Created attachment 221201 [details]
Possible patch

Paul, could you check the proposed patch?
Comment 5 Oleg Besedin CLA 2012-09-20 11:43:38 EDT
Created attachment 221303 [details]
Additional patch for "Show view"

The "Show view" menu itself is also leaking CommandContributionItem's and related element.

Every time the menu is about to be displayed, the list of available views gets reconstructed. The list is associated with a menu manager that never gets released.

The proposed patch remembers the menu manager and releases its elements before a new menu manager is created.

The patch is a "minimal change" approach. I don't think we really need to recreate the menu manager every time, but I tried to keep changes to the minimum to avoid side effects.
Comment 6 Paul Webster CLA 2012-09-20 13:35:03 EDT
(In reply to comment #4)
> Created attachment 221201 [details]
> Possible patch

I would suggest putting the disconnectReferences() in the widget dispose call as well.  It's possible to dispose the widget and the have its fill(*) method called again without disposing the ICI or disconnecting it from its parent contribution manager.

(In reply to comment #5)
> Created attachment 221303 [details]
> Additional patch for "Show view"

This looks good.

PW
Comment 7 Oleg Besedin CLA 2012-09-21 10:19:29 EDT
(In reply to comment #6)
> I would suggest putting the disconnectReferences() in the widget dispose
> call as well. 

Great idea, done!

Change released into R4_2_maintenance:

http://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/?h=R4_2_maintenance&id=c50d62e0e5dbd753e6560440392780b7fdcd6707
Comment 8 Karen Butzke CLA 2012-09-25 14:02:26 EDT
I have done some testing with this patch. While it appears that the steady leaking is gone, I am still seeing odd behavior with CommandContributionItems.

I added a println to CommandContributionItem.update(String)
System.out.println(getClass().getName() + "(id=" + getId() + ", objectid=" + Integer.toHexString(hashCode()) + ")"); 

When I first start eclipse with a Java perspective I can open and close Java editors, switch between Java/Git perspective, switch between Java editors all without seeing any calls to CommandContributionItem.update. (Other than initially building the workspace).

Now if I open the 'Window -> Show view' menu I see calls to that update. Now every time I do any of the above actions I see multiple calls to update. So if I open a java editor I see the following updates called 12 times! Even something like opening the workspace preferences causes these same updates to be called 18 times!

org.eclipse.ui.menus.CommandContributionItem(id=org.eclipse.ant.ui.views.AntView, objectid=1f95fc4)
org.eclipse.ui.menus.CommandContributionItem(id=org.eclipse.ui.views.ResourceNavigator, objectid=1e82e1b)
org.eclipse.ui.menus.CommandContributionItem(id=org.eclipse.ui.views.ProgressView, objectid=2eab5b)
org.eclipse.ui.menus.CommandContributionItem(id=org.eclipse.ui.navigator.ProjectExplorer, objectid=f3a6e)
org.eclipse.ui.menus.CommandContributionItem(id=org.eclipse.ui.views.TaskList, objectid=1e154b9)
org.eclipse.ui.menus.CommandContributionItem(id=org.eclipse.ui.texteditor.TemplatesView, objectid=602764)

Then using the Git perspective really multiplies the problem because so many of their menu items are CommandContributionItems. It just doesn't seem to me that these should need to be updated so many times. Or even updated at all in the above scenarios? Have I found a different bug?
Comment 9 Paul Webster CLA 2012-09-25 14:22:08 EDT
I've opened Bug 390379 to investigate that.

PW
Comment 10 Karen Butzke CLA 2012-09-25 14:32:29 EDT
Thanks Paul, once there is an i-build after SR1 goes out I will test this fix in my every day development.
Comment 11 Karen Butzke CLA 2012-10-05 16:24:29 EDT
Paul, Oleg, things seems *much* better now that I am using a 4.2.2 build for my development. So far I am not seeing the gradual performance degradation!

I am still seeing some room for improvement. I just entered bug 391270 which suggests how to go further with the fix you did here.
Comment 12 Paul Webster CLA 2012-11-16 06:45:27 EST
*** Bug 377119 has been marked as a duplicate of this bug. ***
Comment 13 Paul Webster CLA 2013-01-17 14:03:26 EST
in M20130116-1800
PW