Fixing issue with Classloader leaks.

We had an issue with a classloader leak. There were three things we had to do before it worked.

  1. Made the mysql connector provided and add mysql-connector jar to the tomcat/libexe/lib directory
    1. Maven block:
      <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.34</version>
      <scope>provided test</scope>
      </dependency>
      
  2. Added the following jvm option to use G1 garbage collection, and enable class unloading and perm gen sweep.
    NOTE: You need all three for this to work. Ignore the warning from java: “Please use CMSClassUnloadingEnabled in place of CMSPermGenSweepingEnabled in the future.” I’ve tried just G1, G1 with class unload, and G1 with perm gen. They are all needed for heap to stay low and for classes to be unloaded.

    -XX:+UseG1GC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
  3. Added the following library for classloader leak prevention.
    1. Add the following block in your pom.xml of the web module:
      <!--
      https://github.com/mjiderhamn/classloader-leak-prevention
      -->
      <dependency>
      <groupId>se.jiderhamn</groupId>
      <artifactId>classloader-leak-prevention</artifactId>
      <version>1.15.2</version>
      </dependency>
      
    2. Add the following listener to your web.xml:
      <listener>
      <description>https://github.com/mjiderhamn/classloader-leak-prevention</description>
      <listener-class>se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor</listener-class>
      </listener>
      

Once that is all setup, do a clean package on maven, redeploy and restart tomcat

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s