160715、在web.xml中注册IntrospectorCleanupListener解决Quartz等框架可能产生的内存泄露问题
增加方式如下:web.xml中加入
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
org.springframework.web.util.IntrospectorCleanupListener源代码中对其的解释如下:
在Web应用程序关闭时IntrospectorCleanupListener将会刷新JDK的JavaBeans的Introspector缓存。在你的web.xml中注册这个listener来确保Web应用程序的类加载器以及其加载的类正确的释放资源。
如果JavaBeans的Introspector已被用来分析应用程序类,系统级的Introspector缓存将持有这些类的一个硬引用。因此,这些类和Web应用程序的类加载器在Web应用程序关闭时将不会被垃圾收集器回收!而IntrospectorCleanupListener则会对其进行适当的清理,已使其能够被垃圾收集器回收。
不幸的是,唯一能够清理Introspector的方法是刷新整个Introspector缓存,没有其他办法来确切指定应用程序所引用的类。这将删除所有其他应用程序在服务器的缓存的Introspector结果。
请注意,在使用Spring内部的bean机制时,不需要使用此监听器,因为Spring自己的introspection results cache将会立即刷新被分析过的JavaBeans Introspector cache,而仅仅会在应用程序自己的ClassLoader里面持有一个cache。虽然Spring本身不产生泄漏,注意,即使在Spring框架的类本身驻留在一个“共同”类加载器(如系统的ClassLoader)的情况下,也仍然应该使用使用IntrospectorCleanupListener。在这种情况下,这个IntrospectorCleanupListener将会妥善清理Spring的introspection cache。
应用程序类,几乎不需要直接使用JavaBeans Introspector,所以,通常都不是Introspector resource造成内存泄露。相反,许多库和框架,不清理Introspector,例如: Struts和Quartz。
需要注意的是一个简单Introspector泄漏将会导致整个Web应用程序的类加载器不会被回收!这样做的结果,将会是在web应用程序关闭时,该应用程序所有的静态类资源(比如:单实例对象)都没有得到释放。而导致内存泄露的根本原因其实并不是这些未被回收的类!
IntrospectorCleanupListener应该注册为web.xml中的第一个Listener,在任何其他Listener之前注册,比如在Spring's ContextLoaderListener注册之前,才能确保IntrospectorCleanupListener在Web应用的生命周期适当时机生效。
下面是源码
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "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 has been used to analyze application classes,
* the system-level Introspector cache will hold a hard reference to those classes.
* Consequently, those classes and the web application class loader will not be
* garbage-collected on web app shutdown!</b> This listener performs proper cleanup,
* to allow for garbage collection to take effect.
*
* <p>Unfortunately, the only way to clean up the Introspector is to flush
* the entire cache, as there is no way to specifically determine the
* application's classes referenced there. This will remove cached
* introspection results for all other applications in the server too.
*
* <p>Note that this listener is <i>not</i> necessary when using Spring's beans
* infrastructure within the application, as Spring's own introspection results
* cache will 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 that this
* listener should nevertheless be 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 will properly clean up Spring's introspection cache.
*
* <p>Application classes hardly ever need to use the JavaBeans Introspector
* directly, so are 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 will cause the entire web
* app class loader to not get garbage collected! This has the consequence that
* you will see all the application's static class resources (like singletons)
* around after web app shutdown, which is not the fault of those classes!
*
* <p><b>This listener should be registered as the first one in {@code web.xml},
* before any application listeners such as Spring's ContextLoaderListener.</b>
* This allows the listener to 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();
}
}
注意:quartz默认开启的时候就会启动很多线程
160715、在web.xml中注册IntrospectorCleanupListener解决Quartz等框架可能产生的内存泄露问题的更多相关文章
- 在web.xml中添加配置解决hibernate 懒加载异常
在web.xml添加如下,注意:在配置在struts2的拦截器之前,只能解决请求时出现的懒加载异常:如果没有请求,还需要lazy属性的添加(比如过滤器) <!-- 配置Spring的用于解决懒加 ...
- 关于jsp web项目,jsp页面与servlet数据不同步的解决办法(报错404、405等)即访问.jsp和访问web.xml中注册的/servlet/的区别
报错信息: Type Status Report Message HTTP method GET is not supported by this URL Description The method ...
- SpringMVC: web.xml中声明DispatcherServlet时一定要添加load-on-startup标签
游历SpringMVC源码后发现,在web.xml中注册的ContextLoaderListener监听器只是初始化了一个根上下文,仅仅完成了组件扫描和与容器初始化相关的一些工作,并没有探测到具体每个 ...
- Struts在Web.xml中的配置及Struts1和Struts2的区别
(1)配置Struts的ActionServlet <servlet>元素来声明ActionServlet <servlet-name>元素:用来定义Servle ...
- 解决servlet在web.xml中的路径跳转问题
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" ...
- SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息
在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...
- 使用框架时,在web.xml中配置servlet时,拦截请求/和/*的区别。
关于servlet的拦截设置,之前看了好多,说的都不太清除,明白. 最近明白了一些,总的来说就是保证拦截所有用户请求的同时,放行静态资源. 现整理如下: 一.我们都知道在基于Spring的Applic ...
- 因为此控件已在 web.config 中注册并且与该页位于同一个目录中
在web.config文件配置了用户控件 <pages> <controls> <add tagPrefix="my" tagName="l ...
- web.xml中webAppRootKey
------------------------------------------------------------------------------------------------1. w ...
随机推荐
- NOJ 1012 进制转换(十进制转换成随意进制)
题目: 进制转换 时间限制(普通/Java) : 1000 MS/ 3000 MS 执行内存限制 : 65536 KByte总提交 : 1819 測试通过 : ...
- 轻量集群管理工具PSSH
PSSH 的意思是 Parallel SSH,并行的SSH,很好理解,PSSH 可以让一条命令在多个服务器上同时执行 这就简化了集群的管理工作,例如想查看一下各台服务器现在的负载状况,就可以通过 PS ...
- Vim进阶指南
常用按键说明 按键 解释 移动光标 n+(Space) 向右移动n个字符 n+(Enter) 向下移动n行 nG 移动到第n行 gg 移动到第一行 G 移动到最后一行 0或Home键(Mac使用fn+ ...
- log4cxx在linux下的编译使用
最近在linux下使用log4cxx库,按照其官方文档提供的方法来进行编译,不能成功,又利用google搜索了好几个中文博客上讲述在linux下编译使用log4cxx库的方法,依然不能成功,在这里我奉 ...
- 函数参数中“x++”造成的运算无效测试
可能以前书上都有说过,当时没在意 只有在实际项目中才会遇到因这个问题导致的Bug 2017/2/26日补充:实际上比较通用的做法是 ++tmp1,这样也可以做到自增 ; ); Console.Writ ...
- 使用spring的多线程机制
多线程并发处理起来通常比較麻烦,假设你使用spring容器来管理业务bean,事情就好办了多了.spring封装了java的多线程的实现,你仅仅须要关注于并发事物的流程以及一些并发负载量等特性. 详细 ...
- ACM_数论_阶乘N!的末尾有几个零 和 末尾有多少个 1 nyoj 954
原文地址 首先阶乘的一个常识要知道就是25!的末尾6位全是0: 前言: <编程之美>这本书,爱不释手! 问题描述: 给定一个整数N,那么N的阶乘N!末尾有多少个0呢?例如:N=10,N!= ...
- iOS 新手引导页图片适配及其尺寸大全
早期新手引导页只需要几张图片就可以解决了,随着屏幕尺寸的的越来越多,新手引导页的尺寸适配起来越来越麻烦,否则就会出现尺寸不匹配,图片被拉伸的情况 目前应该是有2种方法来解决这个问题 方法1: 根据每款 ...
- hdu5745 La Vie en rose 巧妙地dp+bitset优化+滚动数组减少内存
/** 题目:hdu5745 La Vie en rose 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5745 题意:题目给出的变换规则其实就是交换相邻 ...
- PHP上传类 图片上传 upload class实现image crop resize 缩略图
manage uploaded files, and manipulate images in many ways through an HTML form, a Flash uploader, XM ...