160715. Register Introspectorcleanuplistener in Web. XML to resolve memory leaks that may arise from frameworks such as quartz

Source: Internet
Author: User

Add the following method: Add to Web. xml
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
The Org.springframework.web.util.IntrospectorCleanupListener source code explains the following:
Introspectorcleanuplistener refreshes the introspector cache of the JDK's JavaBeans when the Web application is closed. Register this listener in your Web. XML to make sure that the class loader for the application and its loaded classes are releasing the resources correctly.
If JavaBeans's introspector has been used to parse the application class, the system-level Introspector cache will hold a hard reference to these classes. Therefore, the class loader for these classes and Web applications will not be reclaimed by the garbage collector when the Web application is closed! Introspectorcleanuplistener, in turn, cleans it up properly, allowing it to be recycled by the garbage collector.
Unfortunately, the only way to clean up introspector is to flush the entire introspector cache, and there is no other way to specify exactly which classes the application refers to. This will remove all other application Introspector results on the server's cache.
Note that it is not necessary to use this listener when using the bean mechanism inside spring, because spring's own introspection results cache will immediately refresh the parsed JavaBeans introspector cache. It only holds a cache in the application's own ClassLoader. Although spring itself does not produce leaks, note that even if the spring framework's class itself resides in a "common" classloader (such as the system's ClassLoader), it should still use Introspectorcleanuplistener. In this case, the Introspectorcleanuplistener will properly clean up the spring introspection cache.
Application classes, which rarely require direct use of the JavaBeans introspector, are usually not introspector resource cause memory leaks. Conversely, many libraries and frameworks do not clean up introspector, for example: struts and quartz.
It is important to note that a simple introspector leak will cause the entire Web application's ClassLoader to not be recycled! The result will be that when the Web application shuts down, all static class resources (such as single-instance objects) of the application are not freed. The root cause of the memory leaks is not the classes that are not recycled!
Introspectorcleanuplistener should be registered as the first listener in Web. XML, registered before any other listener, such as before Spring's contextloaderlistener registration, To ensure that the introspectorcleanuplistener takes effect at the appropriate time in the Web app's life cycle.


Here is the source code
/*
* Copyright 2002-2012 The original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You are not a use this file except in compliance with the License.
* Obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable or agreed to writing, software
* Distributed under the License is distributed on a "as is" BASIS,
* Without warranties or CONDITIONS of any KIND, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/

Package org.springframework.web.util;

Import Java.beans.Introspector;

Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;

Import Org.springframework.beans.CachedIntrospectionResults;

/**
* Listener that flushes the JDK ' s {@link java.beans.Introspector JavaBeans introspector}
* Cache on web App shutdown. Register this listener in your {@code Web. xml} to
* Guarantee proper release of the Web application class loader and its loaded classes.
*
* <p><b>if the JavaBeans introspector have been used to analyze application classes,
* The System-level introspector cache would hold a hard reference to those classes.
* Consequently, those classes and the Web application class loader won't be
* garbage-collected on the Web App shutdown!</b> This listener performs proper cleanup,
* To-allow-garbage collection to take effect.
*
* <p>unfortunately, the only-to-clean-up-the-introspector is-to-flush
* The entire cache, as there is no-to specifically determine the
* application ' s classes referenced there. This would remove cached
* Introspection results for all and applications in the server too.
*
* <p>note that this listener are <i>not</i> necessary when using Spring ' s beans
* Infrastructure within the application, as Spring ' s own introspection results
* Cache would immediately flush an analyzed class from the JavaBeans introspector
* Cache and only hold a cache within the application ' s own ClassLoader.
*
* <b>although Spring itself does not create JDK introspector leaks, note the This
* Listener should nevertheless is used in scenarios where the Spring framework classes
* themselves reside in a ' common ' ClassLoader (such as the system ClassLoader) .</b>
* In such a scenario, this listener'll properly clean up Spring ' s introspection cache.
*
* <p>application classes hardly ever need to use the JavaBeans introspector
* Directly, so is normally not the cause of introspector resource leaks.
* Rather, many libraries and frameworks do not clean up the introspector:
* e.g. Struts and Quartz.
*
* <p>note that a single such introspector leak would cause the entire web
* App class loader to not get garbage collected! This have the consequence that
* You'll see all the application's static class resources (like singletons)
* Around after web App shutdown, which are not the fault of those classes!
*
* <p><b>this Listener should is registered as the first one in {@code Web. xml},
* Before any application listeners such as Spring ' s contextloaderlistener.</b>
* This allows the listener-take full effect at the right time of the lifecycle.
*
* @author Juergen Hoeller
* @since 1.1
* @see java.beans.introspector#flushcaches ()
* @see Org.springframework.beans.cachedintrospectionresults#acceptclassloader
* @see Org.springframework.beans.cachedintrospectionresults#clearclassloader
*/
public class Introspectorcleanuplistener implements Servletcontextlistener {

public void contextinitialized (Servletcontextevent event) {
Cachedintrospectionresults.acceptclassloader (Thread.CurrentThread (). Getcontextclassloader ());
}

public void contextdestroyed (Servletcontextevent event) {
Cachedintrospectionresults.clearclassloader (Thread.CurrentThread (). Getcontextclassloader ());
Introspector.flushcaches ();
}
}

Note: Quartz will start many threads when it is turned on by default

160715. Register Introspectorcleanuplistener in Web. XML to resolve memory leaks that may arise from frameworks such as quartz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.