Java – JMX: how to prevent classloader memory leakage in servlet container?
I want to know whether or how to handle MBeans registered directly or indirectly from applications deployed on servlet containers
In most cases, there are two ways to retrieve the mbeanserver available for registration
>Use mbeanserverfactory Creatembeanserver() create your own mbeanserver > use managementfactory getPlatformMBeanServer()
When using the first option, it is easy to log off all MBeans: just call mbeanserver releaseMBeanServer(myMBeanServer).
But what about the second option often used in third-party applications? (and btw, which is also the method recommended by sun / Oracle)
Because the platform mbeanserver is used, when the servlet context is broken, it will not be unregistered, but worse, it still retains the reference to the web application class loader Therefore, all static references of the web application will not be released, resulting in leakage
If you want to test this: just deploy a simple web application, which allocates a 100MB array. This array is statically referenced. It uses an Oracle jdbc driver (it will register a diagnostic MBean with the platform MBean server) and deploy it on Tomcat Stop the application and restart it – repeat this operation and you will type outofmemoryerror
Question:
>Do I usually deal with these problems, or do I deal with servlet containers and / or third-party libraries? > Is there any way to obtain all MBeans of mbeanserver, and which classes are loaded by a specific classloader? > What can I do to prevent this? Must I track all registered MBeans to the platform mbeanserver and cancel the registration in contextdestroyed()?
Solution
This is my standard suggestion I don't know a better choice