【问题标题】:Tomcat not unloading classes when aws s3 client is used使用 aws s3 客户端时,Tomcat 不卸载类
【发布时间】:2018-09-28 20:01:28
【问题描述】:

我在使用 JAVA AWS s3 客户端时看到一个奇怪的问题。当我进行多次部署时(tomcat 没有重新启动,只是更新了 war 文件),堆大小保持不变,但非堆大小不断增加。结果是在取消部署应用程序时没有卸载类。

我的应用程序有一个简单的上下文监听器,它初始化一个 AWS S3 客户端并在应用程序上下文被销毁时将其关闭。

代码如下:

@WebListener
public class ContainerContextClosedHandler implements ServletContextListener {

    private static AmazonS3 s3Client;


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        s3Client = AmazonS3ClientBuilder.standard().build();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        try {
            System.out.println("shutting down s3Client");
            if (s3Client != null) {
                s3Client.shutdown();
            }
            com.amazonaws.http.IdleConnectionReaper.shutdown();
        } catch (Throwable t) {
            // log the error
        }
    }
}

取消部署应用程序时如何卸载类。

【问题讨论】:

  • 嗨 Ajith,您能否按照java.jiderhamn.se/2011/12/11/… 的说明进行操作,以便我们查看导致泄漏的原因?如果 AWS S3 库中确实存在泄漏,我很乐意在我的 ClassLoader Leak Prevention 库中添加对此的支持 github.com/mjiderhamn/classloader-leak-prevention
  • 嗨 Mattias,我使用你的库来检测泄漏。这是我在我的 tomcat 取消部署日志中得到的。 se.jiderhamn.classloader.leak.prevention.JULLogger.warn MBean 'com.amazonaws.management:type=AwsSdkMetrics' was loaded by protected ClassLoader; unregistering se.jiderhamn.classloader.leak.prevention.JULLogger.error Internal registry of java.beans.PropertyEditorManager not found Waiting for Thread 'Thread[java-sdk-http-connection-reaper,5,main]' of type com.amazonaws.http.IdleConnectionReaper loaded by protected ClassLoader with contextClassLoade

标签: java amazon-s3 tomcat8 heap-memory permgen


【解决方案1】:

正如我在之前的评论中所说,我发现 AwsSdkMetrics bean 是罪魁祸首。 所以我在我的 contextDestroyed 方法中添加了这个语句

AwsSdkMetrics.unregisterMetricAdminMBean();

取消注册 MetricAdminMBean。

非常感谢 Mattias 推荐他的精彩图书馆,帮助我找出根本原因。

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    相关资源
    最近更新 更多