title: Class reloading leak on tomcat
body: <p>I take the following steps:</p>
  
  <ol>
  <li>Create simplest servlet and force several times to reload (by modify) tomcat app, to make sure it's not tomcat realated.</li>
  <li>Add jars to WEB-INF/lib (spring, db2 driver, tomcat-jdbc, hibernate) to make sure that some of classes do not create singletons which are pain if we speak about realoading.</li>
  </ol>
  
  <p>But in both case number of loaded classes where rather stable it's vary, but haven't seen anything abnormal.</p>
  
  <p>So then I tried to register Spring, and DataSource + Hibernate config files. Here's what happening after several reloads:</p>
  
  <p><img src="https://i.stack.imgur.com/KAJYP.png" alt="enter image description here"></p>
  
  <p>Here goes my web.xml &amp; mvc-spring.xml which seems to create this class leak:</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
      &lt;display-name&gt;SpringMVC&lt;/display-name&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/mvc-dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;encoding&lt;/param-name&gt;
              &lt;param-value&gt;UTF-8&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>spring-mvc</strong></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
      &lt;context:component-scan base-package="my.spring" /&gt;
  
      &lt;!-- Required for MVC Spring to use annotations --&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;!-- JDBC Connection --&gt;
      &lt;bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"&gt;
          &lt;!-- Credentials --&gt;
          &lt;property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" /&gt;
          &lt;property name="url" value="jdbc:url" /&gt;
          &lt;property name="username" value="db2admin" /&gt;
          &lt;property name="password" value="password" /&gt;
  
          &lt;!-- Settings --&gt;
          &lt;property name="initialSize" value="5" /&gt;
          &lt;property name="maxActive" value="10" /&gt;
          &lt;property name="maxIdle" value="5" /&gt;
          &lt;property name="minIdle" value="2" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy"&gt;
          &lt;property name="dataSource"&gt;
              &lt;ref bean="dataSource" /&gt;
          &lt;/property&gt;
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.DB2Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Any idea why this happens?</p>
  
  <p><strong>UPDATE</strong>
  I found following options <code>-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC</code> passed as VM arguments. To be honest neither I know how they work nor why they aren't turn on by default. But they seems to solve the problem.</p>