热重新部署和 Oracle 数据库导致永久内存泄漏

pao*_*oyx 5 oracle tomcat memory-leaks permgen spring-roo

我有一个在生产环境中运行的基于 SpringRoo 的应用程序,在一些热重新部署后导致严重的永久内存泄漏。

为了“查找并修复”泄漏并减少分析过程中的变量,我使用 roo 创建了一个简单、精简的应用程序,并且获得了相同的行为。该项目(使用 Spring Roo (1.2.3.RELEASE) 创建)只是持久化一个名为“Person”的实体和一个名为“name”的字符串字段。

我在 Tomcat 7.0.39 上部署 war,使用 Oracle 11.2.0.2 作为数据库。每次重新部署后,我都会在 catalina.out 中收到此消息

INFO: Undeploying context [/ojdbc-0.1.0.BUILD-SNAPSHOT]
mag 06, 2013 10:50:43 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/ojdbc-0.1.0.BUILD-SNAPSHOT] registered the JDBC driver       [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped.    To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Run Code Online (Sandbox Code Playgroud)

两次热重新部署后,我收到一个permgen 错误

mag 06, 2013 10:51:08 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /Applications/apache-tomcat-7.0.39/webapps/ojdbc-    0.1.0.BUILD-SNAPSHOT.war
Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]"
Exception in thread "RMI TCP Connection(idle)" mag 06, 2013 10:51:17 AM ServerCommunicatorAdmin reqIncoming
WARNING: The server has decided to close this client connection.
java.lang.OutOfMemoryError: PermGen space
Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用 VisualVm / EclipseMemory Analyzer 对此进行分析,这就是我目前得到的结果。

具有 PermGen 行为的 VisualVM 截图

从支配树分析中获得的 GC Roots

事实是,我没有观察到其他数据库(例如 PostgreSQL 或 Hypersonic)的这种行为。是否有与 Oracle 相关的内容导致泄漏?

是一个包含 roo 脚本生成器的 zip 存档,anche .hprof 转储文件。

Rya*_*yan 0

尝试将 Oracle JDBC 驱动程序移至 Tomcat 的 lib 目录中,而不是移至应用程序的 lib 文件夹内。看起来 OracleDiagnosabilityMBean 正在处理 Catalina。

编辑:由于您无法控制 Tomcat,请尝试像这样包装 Oracle 类的加载位置(除了替换 Oracle init 的 AppContext 之外):

http://cdivilly.wordpress.com/2012/04/23/permgen-memory-leak/

//somewhere in application startup, e.g. the ServletContextListener
try {
 final ClassLoader active = Thread.currentThread().getContextClassLoader();
 try {
  //Find the root classloader
  ClassLoader root = active;
  while (root.getParent() != null) {
   root = root.getParent();
  }
  //Temporarily make the root class loader the active class loader
  Thread.currentThread().setContextClassLoader(root);
  //Force the AppContext singleton to be created and initialized
  sun.awt.AppContext.getAppContext();
 } finally {
 //restore the class loader
 Thread.currentThread().setContextClassLoader(active);   
}
} catch ( Throwable t) {
   //Carry on if we get an error
   LOG.warning("Failed to address PermGen leak");
}
Run Code Online (Sandbox Code Playgroud)