tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lionel Farbos <lionel.far...@free.fr>
Subject Re: Singleton memory leak after redeploying.
Date Mon, 28 Nov 2005 17:24:52 GMT
Hi Juan,

I think your problem can't be solved (only) by Tomcat.
The problem is that objets stay in memory because of pointers on static references...

To solve this :
You can embed a CleanupListener in your webapp.
This listener seem to be like this :
------------
package com.yourWebApp;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class CleanupListener implements ServletContextListener 
{
  public MyCleanupListener() { super(); }
  public void contextInitialized(ServletContextEvent event) { }

  public void contextDestroyed(ServletContextEvent event) {
    MySingleton.delInstance();
  }
}
-----------

Before this, you have to 
1) modify your MySingleton with :

public static void delInstance()
{
  if (_instance != null) { _instance._charWidths=null; }
  _instance = null;
}
2) add, in web.xml :
<listener>
  <listener-class>com.yourWebApp.CleanupListener</listener-class>
</listener>

Notes :
- Perhaps your Listener will have to delete other objets like SQL drivers, commons logger,
...
- see also : http://opensource2.atlassian.com/confluence/spring/pages/viewpage.action?pageId=2669

Regards.

On Mon, 28 Nov 2005 08:29:13 +0000
kurrele <kurrele@gmail.com> wrote:

> Hi to everyone!! This is my first email to this list, hope is the correct
> one.
> 
> I've been analyzing my code with a profiler (JProbe) because I was getting
> an
> OutOfMemoryException after a few redeploys of my application.
> 
> After some tests, it turned out that my static fields were not GCed after
> redeploying, getting stuck with the classloader which loaded my classes.
> 
> After that, I searched the bugzilla and found
>   *Bug# *(Memory Leak in Classloader/Manager deploy/undeploy)*:*
> 20758<http://issues.apache.org/bugzilla/show_bug.cgi?id=20758>
> where it says that the bug has been FIXED.
> 
> I tested it in Tomcat 4.1.24 and 5.0.28 getting the same result (memory leak
> in static fields).
> 
> I attach here the servlet and singleton of my simple test webapp.
> 
> ---------------------------------------------------------------------------------------------------------------------------
> public class Test extends HttpServlet
> {
> 
>     MySingleton singleton = MySingleton.getInstance();
> 
>     public void init(ServletConfig config) throws ServletException
>     {
>         super.init(config);
> 
>     }
> 
>     public void destroy()
>     {
>         singleton = null;
>     }
> 
>     protected void processRequest(HttpServletRequest request,
> HttpServletResponse response)
>     throws ServletException, IOException
>     {
>         response.setContentType("text/html");
>         PrintWriter out = response.getWriter();
>         out.println("<html><head><title>JProbe TEST
> singleton</title></head><body><b>JProbe TEST
> singleton</b><BR></body></html>");
>         out.close();
>     }
>     protected void doGet(HttpServletRequest request, HttpServletResponse
> response)
>     throws ServletException, IOException
>     {
>         processRequest(request, response);
>     }
> 
>     protected void doPost(HttpServletRequest request, HttpServletResponse
> response)
>     throws ServletException, IOException
>     {
>         processRequest(request, response);
>     }
> }
> 
> 
> -------------------------------------------------------------------------------------------------------------------------------------
> public class MySingleton
> {
> 
>     private static MySingleton _instance = new MySingleton();
> 
>     public byte[] _charWidths = new byte[1024*1024];  // 1Meg mem
> allocation!!.
> 
>     private MySingleton()
>     {}
> 
>     public static MySingleton getInstance()
>     {
>         return _instance;
>     }
> 
>     public void finalize()
>     {
>         System.out.println("!!! END-MySingleton !!!");
>         _instance = null;
>     }
> }
> 
> 
> -------------------------------------------------------------------------------------------------------------------------------------
> 
> After redeploying this simple webapp a 1Meg leak can be found due to
> MySingleton not being GCed.
> 
> Please I want to know If I'm leaving out something, the bug is not really
> fixed or I need to take another approach.
> 
> Thanks.
> 
>  Juan F. S. (Spain).
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message