United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6482247 Using MXBeans can lead to memory leaks
JDK-6482247 : Using MXBeans can lead to memory leaks

Details
Type:
Bug
Submit Date:
2006-10-16
Status:
Resolved
Updated Date:
2010-07-29
Project Name:
JDK
Resolved Date:
2006-12-12
Component:
core-svc
OS:
generic
Sub-Component:
javax.management
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:
6u1 (b01)

Related Reports
Backport:

Sub Tasks

Description
I ran a simple test that creates a ClassLoader, executes some MXBean code using the ClassLoader, then waits for the ClassLoader to be garbage-collected.  The ClassLoader is *not* garbage-collected, because of two problems:

(1) The class com.sun.jmx.mbeanserver.MXBeanLookup keeps a WeakHashMap<MBeanServerConnection, MXBeanLookup> in order to find the lookup table for a given MBeanServerConnection.  Since MXBeanLookup has a reference to its MBeanServerConnection, entries in this WeakHashMap never go away.  This means that if you create a number of connections to remote MBeanServers over the lifetime of an application, and you use those connections to navigate MXBean references, then you will build up entries in the WeakHashMap.

(2) The class javax.management.MBeanServerInvocationHandler keeps a WeakHashMap<Class<?>, MXBeanProxy> in order to avoid having to reapply the introspection logic on an MXBean interface every time that interface is used to make a proxy.  Since MXBeanProxy has a field Map<Method, Handler> and the Methods in question come from the Class<?> that is a key in the WeakHashMap, again we have the classic bug where the values in a WeakHashMap strongly-reference the keys.  This means that if you run some code from a ClassLoader that creates proxies using interfaces defined by that ClassLoader, then the ClassLoader will never be gc'd.

                                    

Comments
EVALUATION

The fix is straightforward and should be applied as soon as practical.
                                     
2006-10-16
SUGGESTED FIX

Wrap the values of the two WeakHashMaps in question inside WeakReferences.
                                     
2006-10-16



Hardware and Software, Engineered to Work Together