JDK-6482247 : Using MXBeans can lead to memory leaks
  • Type: Bug
  • Component: core-svc
  • Sub-Component: javax.management
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-10-16
  • Updated: 2010-07-29
  • Resolved: 2006-12-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u1 b01Fixed 7Fixed
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
SUGGESTED FIX Wrap the values of the two WeakHashMaps in question inside WeakReferences.
16-10-2006

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