Bug 398568 - Memory leak in org.eclipse.jem.workbench.utility.JavaModelListener
Summary: Memory leak in org.eclipse.jem.workbench.utility.JavaModelListener
Status: NEW
Alias: None
Product: WTP Java EE Tools
Classification: WebTools
Component: jst.jem (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: Future   Edit
Assignee: jst.j2ee CLA
QA Contact: Chuck Bridgham CLA
URL:
Whiteboard:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2013-01-19 12:32 EST by Snjezana Peco CLA
Modified: 2015-01-29 13:55 EST (History)
2 users (show)

See Also:


Attachments
A patch (2.38 KB, patch)
2013-01-19 12:35 EST, Snjezana Peco CLA
no flags Details | Diff
A patch (2.39 KB, patch)
2014-02-07 17:51 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-19 12:32:31 EST
The JavaModelListener uses the resolvedContainers hash map to cache some classpath container entries which causes a memory leak when using m2e and creating an EAR project.
M2e updates a classpath using the following code: (http://git.eclipse.org/c/m2e/m2e-core.git/tree/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/BuildPathManager.java):

IClasspathEntry[] classpath = getClasspath(project, monitor);
IClasspathContainer container = new MavenClasspathContainer(path, classpath);

when updating the project's classpath.

This code doesn't create ElementChangedEvent of the IJavaElementDelta.F_RESOLVED_CLASSPATH_CHANGED type because neither the container's path nor the container's attributes are changed. That's why the resolvedContainers hash map isn't cleared (JavaModelListener.java, lines 107-109). 
However, this map doesn't contain the project's classpath container anymore and a new container is added to the map (JavaModeListener.java, lines 361-365).
The map will leak the old Maven classpath container and all its entries. For instance, rebuilding the project created using the org.jboss.spec.archetypes:jboss-javaee6-webapp-ear-archetype maven archetype will leak several thousands IClasspathEntry/IClasspathAttribute objects.

I think, the issue can be solved in two ways:

1. using the container's path instead of container as a key for the resolvedContainers hash map
In my opinion, this is not a good solution because some container can return a different result when ever its getClasspathEntries() method is called.

2. removing the resolvedContainers hash map completely and letting classpath containers to cache their entries.

Attached is a patch that uses case #2.
Comment 1 Snjezana Peco CLA 2013-01-19 12:35:26 EST
Created attachment 225855 [details]
A patch
Comment 2 Snjezana Peco CLA 2014-02-07 17:51:44 EST
Created attachment 239758 [details]
A patch

The problem is still reproducible in Luna. I have rebased the patch.
Comment 3 Rob Stryker CLA 2014-05-07 03:44:57 EDT
A memory leak is a pretty critical issue that can eventually lead to performance issues the longer a user keeps his workspace open, leading to the impression that the product is slow and bloated. 

Bumping severity.
Comment 4 Roberto Sanchez Herrera CLA 2014-05-07 11:37:44 EDT
Hi, 
Could you attach a project or projects to recreate the problem? and the steps to recreate the issue? For example, import projects, then add a dependency to pom of project x.
Comment 5 Roberto Sanchez Herrera CLA 2014-05-15 11:21:58 EDT
Hi, 
The reason I asked for the steps to recreate the problem is that I want to see if another type of event is triggered when the Maven classpath container is updated, and maybe we can check in JavaModelListener for that event besides IJavaElementDelta.F_RESOLVED_CLASSPATH_CHANGED 

The attached patch basically removes the fix for bug 236434, so in my opinion, applying this patch would fix a memory leak but bring back another performance issue.
Comment 6 Roberto Sanchez Herrera CLA 2014-05-29 14:08:30 EDT
During the WTP status meeting was decided to reduce the severity. Reducing to Major
Comment 7 Snjezana Peco CLA 2014-07-30 11:25:11 EDT
(In reply to Roberto Sanchez Herrera from comment #4)
> Hi, 
> Could you attach a project or projects to recreate the problem? and the
> steps to recreate the issue? For example, import projects, then add a
> dependency to pom of project x.

You can create a project using the org.jboss.spec.archetypes:jboss-javaee6-webapp-ear-archetype maven archetype.
M2 is required in order to reproduce the issue.

The fix for bug 236434 will help if there are classpath containers that don't cache the classpath entries. If a container doesn't cache its entries, that will cause bad performance in other parts of Eclipse. 
Also, you can't be sure that classpath containers will always return the same entries so that this fix can disrupt a functionality. That's why I haven't used the solution #1 from https://bugs.eclipse.org/bugs/show_bug.cgi?id=398568#c0.