---
title: "IntrospectorCleanupListener使用"
author: "east_java"
date: "2010-07-19T10:49:30Z"
tags: []
categories: ["企业架构"]
slug: "introspectorcleanuplistener"
draft: false
lastmod: "2010-07-19T10:52:51Z"
id: 715859
url: "https://japi.iteye.com/blog/715859"
---

刚发现一个异常：

```
Exception in thread "sfb_QuartzSchedulerThread" java.lang.OutOfMemoryError: Java heap space
at java.util.TreeMap.key(TreeMap.java:1206)
at java.util.TreeMap.firstKey(TreeMap.java:267)
at java.util.TreeSet.first(TreeSet.java:377)
at org.quartz.simpl.RAMJobStore.acquireNextTrigger(RAMJobStore.java:1209)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:266)
```

 

看这个信息，就是quartz引起了内存溢出问题。

网上资料都说IntrospectorCleanupListener能解决这种问题，但我的web.xml中是配置了这个监听器的。

网上的资料：

spring 中的提供了一个名为org.springframework.web.util.IntrospectorCleanupListener的监听器。它主 要负责处理由　JavaBeans Introspector的使用而引起的缓冲泄露。spring中对它的描述如下：

 

它是一个在web应用关闭的时候,清除JavaBeans Introspector的监听器.在web.xml中注册这个listener.可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 和由它管理的类

 

如果你使用了JavaBeans Introspector来分析应用中的类,Introspector 缓冲中会保留这些类的引用.结果在你的应用关闭的时候,这些类以及web 应用相关的class loader没有被垃圾回收.

 

不幸的是,清除Introspector的唯一方式是刷新整个缓冲.这是因为我们没法判断哪些是属于你的应用的引用.所以删除被缓冲的 introspection会导致把这台电脑上的所有应用的introspection都删掉.

 

需要注意的是,spring 托管的bean不需要使用这个监听器.因为spring它自己的introspection所使用的缓冲在分析完一个类之后会被马上从javaBeans Introspector缓冲中清除掉.

 

应用程序中的类从来不直接使用JavaBeans Introspector.所以他们一般不会导致内部查看资源泄露.但是一些类库和框架往往会产生这个问题.例如:Struts 和Quartz.

 

单个的内部查看泄漏会导致整个的web应用的类加载器不能进行垃圾回收.在web应用关闭之后,你会看到此应用的所有静态类资源(例如单例). 这个错误当然不是由这个类自身引起的.

 

对于这个解释，有点不明白了，为什么是在web应用关闭的时候才清除。web应用都关闭了，内存还不会释放吗？奇怪。

 

还是看看官方解释吧。

Listener that flushes the JDK's [`JavaBeans Introspector`](http://java.sun.com/javase/6/docs/api/java/beans/Introspector.html?is-external=true "class or interface in  java.beans") cache on web app shutdown. Register this listener in your `web.xml` to guarantee proper release of the web application class loader and its loaded classes.

**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!** This listener performs proper cleanup, to allow for garbage collection to take effect.

 

[**全文完整版查看**](http://java1995.cn/blog/item/320)