简体   繁体   中英

Tomcat not unloading classes when aws s3 client is used

I am seeing a strange problem when using JAVA AWS s3 client. When I make multiple deployments(tomcat is not restarted, just war files are updated), the heap size remains the same but non heap size keeps on increasing. Turns out the classes are not unloaded when the application is undeployed.

My application has a simple context listener, which initialises an AWS S3 client and shuts it down when the application context is destroyed.

Here is the code:

@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
        }
    }
}

How can I unload the classes when the application is undeployed.

As posted in my previous comment, I found that AwsSdkMetrics bean was the culprit. So I added this statement in my contextDestroyed method

AwsSdkMetrics.unregisterMetricAdminMBean();

to unregister MetricAdminMBean.

Thanks a ton to Mattias for suggesting his wonderful library which helped me figure the root cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM