0

ContextLoaderListener initializes and closes the root web application context.

What mechanism, if any, is required to close the child WebApplicationContext created by the DispatcherServlet?

web.xml:

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>                      
      /WEB-INF/applicationContext.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/myservlet-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

I'm seeing classes hanging around after stopping applications in Tomcat, wonder if this would help?

Edit: I was never able to find the classes, please see my question for details: no-gc-root-found

Thank you.

The Application Details (in hope of getting a clue to solve permgen) Spring 3.2.1 Spring security 3.1.3 Hibernate 4.1.9 Tomcat 7.42

The application's internal name is 'derby', not to be confused with the DB.

pom.xml

    <dependencies>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        <!-- <scope>provided</scope> -->
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.5</version>
        <exclusions>
            <exclusion>
                <groupId>javax.activation</groupId>
                <artifactId>activation</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.0.2-FINAL</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-digester3</artifactId>
        <version>3.2</version>
        <classifier>with-deps</classifier>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>${org.apache.tiles.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${org.apache.tiles.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-api</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-template</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.2</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-ext</artifactId>
        <version>1.7.2</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.2</version>
    </dependency>

    <dependency>
        <groupId>net.sf.dozer</groupId>
        <artifactId>dozer</artifactId>
        <version>5.4.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.11</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.4.9</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-mail</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-acl</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>
</dependencies>

I used to create DB Connection Pool within Spring, have moved it to META-INF/context.xml hoping to get rid of any lingering Thread created by the Pool.

catalina.out after shutdown.bat is run

Sep 12, 2013 12:44:15 PM org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.27 using APR version 1.4.6.
Sep 12, 2013 12:44:15 PM org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random  [true].
Sep 12, 2013 12:44:15 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized (OpenSSL 1.0.1d 5 Feb 2013)
Sep 12, 2013 12:44:16 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-apr-8188"]
Sep 12, 2013 12:44:16 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-apr-8009"]
Sep 12, 2013 12:44:16 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1441 ms
Sep 12, 2013 12:44:16 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 12, 2013 12:44:16 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.42
Sep 12, 2013 12:44:16 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive J:\Java\Tomcat\apache-tomcat-7.0.42\webapps\derby.war
Sep 12, 2013 12:44:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory J:\Java\Tomcat\apache-tomcat-7.0.42\webapps\manager
Sep 12, 2013 12:44:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory J:\Java\Tomcat\apache-tomcat-7.0.42\webapps\ROOT
Sep 12, 2013 12:44:28 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-apr-8188"]
Sep 12, 2013 12:44:28 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-apr-8009"]
Sep 12, 2013 12:44:28 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 12691 ms
Sep 12, 2013 12:45:14 PM org.apache.catalina.core.StandardServer await
INFO: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
Sep 12, 2013 12:45:14 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-apr-8188"]
Sep 12, 2013 12:45:14 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-apr-8009"]
Sep 12, 2013 12:45:14 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Sep 12, 2013 12:45:15 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-apr-8188"]
Sep 12, 2013 12:45:15 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["ajp-apr-8009"]
Sep 12, 2013 12:45:15 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-apr-8188"]
Sep 12, 2013 12:45:15 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-apr-8009"]

Tomcat Manager shows this message after 'Find leaks' is clicked. enter image description here

Community
  • 1
  • 1
kmansoor
  • 4,265
  • 9
  • 52
  • 95
  • Can you post the classes you see, maybe in a log? – Sotirios Delimanolis Sep 11 '13 at 20:28
  • @Sotirios Delimanolis please see my update, I was never able to hunt down the lingering classes. – kmansoor Sep 11 '13 at 20:34
  • When you `shutdown.sh`, can you post what the `catalina.out` logs? – Sotirios Delimanolis Sep 11 '13 at 20:36
  • You accepted the answer, but I don't know if it solves anything. You might want to consider putting the log. I've had issues like this before. – Sotirios Delimanolis Sep 11 '13 at 21:04
  • Are you using commons-logging and/or cglib? Sometimes that leads to static references to classloaders, keeping everything (or a lot) in memory. – M. Deinum Sep 12 '13 at 07:06
  • @Sotirios Delimanolis, I accepted your answer as it expressely answered the question. But no, my wider woes, permgen, is still unsolved. I have updated the question with pom and the log. If you could please take a look? Thanks. – kmansoor Sep 12 '13 at 16:53
  • @M. Deinum, yes I do see them in the 'resolved dependencies' tab when viewing pom.xml in eclipse. – kmansoor Sep 12 '13 at 16:54
  • Your application shutsdown gracefully from what I can see in the logs. I guess the leak is normal if you only stopped `derby`, but the others are still running. In other words, the class loader has loaded the class required by `derby`, but doesn't unload them after it stops it. I don't know that it even can... – Sotirios Delimanolis Sep 12 '13 at 17:04
  • You are using SLF4J next to commons-logging. Start with excluding commons-logging and include the `jcl-over-slf4j` dependency from org.slf4j. That way you have only a single logging abstraction. http://stackoverflow.com/questions/5635255/permgen-out-of-memory-reasons might be related. – M. Deinum Sep 13 '13 at 06:11
  • Removed commons-logging, but classes still linger around. Thanks for the suggestion though. – kmansoor Sep 13 '13 at 18:04

1 Answers1

1

The DispatcherServlet (through FrameworkServlet) also closes its WebApplicationContext

@Override
public void destroy() {
    getServletContext().log("Destroying Spring FrameworkServlet '" + getServletName() + "'");
    if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
        ((ConfigurableApplicationContext) this.webApplicationContext).close();
    }
}

The classes you are seeing are most likely Threads leftover from a connection pool or possibly an ExecutorService. There are dirty ways to force those to close as well.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724