4

I am (test) running my web app in Tomcat 7 and using their "Find Leak" button and of course it complains about memory leak when I stop / undeploy.

The following web applications were stopped (reloaded, undeployed), but their classes from previous runs are still loaded in memory, thus causing a memory leak (use a profiler to confirm): /LeakyWebApp

So I used Java VisualVM (my first time trying this out)

on Tomcat startup with no deployment: http://img15.imageshack.us/img15/4441/tomcatstartup.jpg

My web app involves:

  • Quartz 1.8.5
  • Hibernate 3.6.3
  • JAXB 2.2.4
  • Salesforce API
  • log4j

Immediately after deployment: http://img850.imageshack.us/img850/2951/tomcatafterdeployment.jpg

So I noticed it complains about Quartz and I also read somewhere to close Hibernate Session Factory on servlet destroy.

On stop / undeploy, The Visual VM does show the Quartz thread stopped, but the tomcat log says

"appears to have start a thread named ... and has failed to stop it"

So I created a new ServletContextListener and on contextDestroyed I call to Quartz factory scheduler to shutdown and also call close on Hibernate Session Factory. and doing another deploy/undeploy, there's no more complain from the tomcat log about Quartz thread problem as above.

However, when I use "find leaks" it still complains about the same thing

The following web applications were stopped (reloaded, undeployed), but their classes from previous runs are still loaded in memory, thus causing a memory leak (use a profiler to confirm): /LeakyWebApp

Then i found another complain about JDBC driver (I have mysql connector jar in my war), so I tried removing that, the complain from tomcat log disappear but the "Find Leaks" still say the same thing that my web app has memory leaks

So my question would be - what else should I be looking at? and/or how can I use the Visual VM better to detect what's going?

Thanks

EDIT: I fixed the issue with Quartz based on the post by David Feitosa, I was missing

 <init-param>
     <param-name>wait-on-shutdown</param-name>
     <param-value>true</param-value>
 </init-param>

from the Quartz properties in web.xml.

However - I still have issue with the JDBC driver - I need it for my web app, and seems like I have 2 solutions based on the answers from: To prevent a memory leak, the JDBC Driver has been forcibly unregistered

  1. Put the mysql-connector jar in the tomcat/lib
  2. Manually un-register the driver in the contextDestroyed.

Which way should I go and what's the best practice for this?

Community
  • 1
  • 1
TS-
  • 4,311
  • 8
  • 40
  • 52

1 Answers1

4

Most of the problems with memory leaks that I found in this kind of web app is related to Quartz. In order to solve this, try to use the proper Quartz Servlet to init the factory: http://quartz-scheduler.org/api/2.0.0/org/quartz/ee/servlet/QuartzInitializerServlet.html

As in the Quartz doc, try to use:

 <servlet>
     <servlet-name>
         QuartzInitializer
     </servlet-name>
     <display-name>
         Quartz Initializer Servlet
     </display-name>
     <servlet-class>
         org.quartz.ee.servlet.QuartzInitializerServlet
     </servlet-class>
     <load-on-startup>
         1
     </load-on-startup>
     <init-param>
         <param-name>config-file</param-name>
         <param-value>/some/path/my_quartz.properties</param-value>
     </init-param>
     <init-param>
         <param-name>shutdown-on-unload</param-name>
         <param-value>true</param-value>
     </init-param>
     <init-param>
         <param-name>wait-on-shutdown</param-name>
         <param-value>true</param-value>
     </init-param>
     <init-param>
         <param-name>start-scheduler-on-load</param-name>
         <param-value>true</param-value>
     </init-param>
 </servlet>

Hope this can help you.

  • thanks, I was only missing the "wait-on-shutdown" property and it fixes the issue. But, I still have an issue with the JDBC driver "[leakyApp] register a JDBC driver but failed to unregister it" - this comes from the fact that I have mysql-connector-java.jar in my web app, but I can't remove it because I need it - what can I do for this problem? – TS- Apr 24 '12 at 15:00
  • Unhappy, I have no clues for the driver problem yet. If I find, I return here. Did you could look in your server documentation? Maybe using a DataSource object as in Tomcat or JBoss can solve your problem with the driver. –  Apr 24 '12 at 15:21
  • HI i am facing the same problem,but how can i add this Servlet to the my application ..can i create new servlet class,and deploy the war file into tomcat.. will it be success.... – Kanchetianeel Dec 03 '14 at 04:48