You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by kurrele <ku...@gmail.com> on 2005/11/28 09:29:13 UTC

Singleton memory leak after redeploying.

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).

Re: Singleton memory leak after redeploying.

Posted by Lionel Farbos <li...@free.fr>.
Hi Rémy,

It's a very good news for all !!!
Until now, we used a CleanupListener to do such things, and webapps could use it or not. 
Now, Tomcat will do this automatically for us :-))

But I need some complements :
- this new feature will be integrated in Tomcat 3 ? Tomcat 4 ? Tomcat 5.0 ?
- in the clearReferences(), you put static and final fields to null; perhaps (?) this can have bad side effects on webapps ?...
  For example, if a thread is not stopped and envisage to do something when such static variable is null ...
  It's not a classic need ... but, perhaps, it is a real use case ?...
- So, will it be possible to configure this new feature on such contexts ?
- My webapps generally don't use commons-logging : they use log4J; so, LogFactory.release will throw an exception. No ?

In advance, Thank you.
Regards.

On Wed, 30 Nov 2005 14:29:15 +0100
Remy Maucherat <re...@gmail.com> wrote:

> On 11/30/05, Remy Maucherat <re...@gmail.com> wrote:
> > This issue also affects Hibernate. As it doesn't seem to be a Tomcat
> > bug, but would be good to have a fix for, I've added possible
> > workarounds for that (reflection code which sets as many static fields
> > as possible to null in loaded classes when stopping the classloader)
> > in the latest Tomcat code (which you need to get from SVN). It would
> > need testing.
> 
> To test this, recompile the class here and replace the original in
> catalina.jar (or put in in the appropriate folder under
> server/classes):
> http://svn.apache.org/viewcvs.cgi/*checkout*/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/loader/WebappClassLoader.java?rev=348448&content-type=text%2Fplain
> 
> --
> xxxxxxxxxxxxxxxxxxxxxxxxx
> Rémy Maucherat
> Developer & Consultant
> JBoss Group (Europe) SàRL
> xxxxxxxxxxxxxxxxxxxxxxxxx
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 

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


Re: Singleton memory leak after redeploying.

Posted by Remy Maucherat <re...@gmail.com>.
On 11/30/05, Remy Maucherat <re...@gmail.com> wrote:
> This issue also affects Hibernate. As it doesn't seem to be a Tomcat
> bug, but would be good to have a fix for, I've added possible
> workarounds for that (reflection code which sets as many static fields
> as possible to null in loaded classes when stopping the classloader)
> in the latest Tomcat code (which you need to get from SVN). It would
> need testing.

To test this, recompile the class here and replace the original in
catalina.jar (or put in in the appropriate folder under
server/classes):
http://svn.apache.org/viewcvs.cgi/*checkout*/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/loader/WebappClassLoader.java?rev=348448&content-type=text%2Fplain

--
xxxxxxxxxxxxxxxxxxxxxxxxx
Rémy Maucherat
Developer & Consultant
JBoss Group (Europe) SàRL
xxxxxxxxxxxxxxxxxxxxxxxxx

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


Re: Singleton memory leak after redeploying.

Posted by Remy Maucherat <re...@gmail.com>.
On 11/28/05, kurrele <ku...@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.

This issue also affects Hibernate. As it doesn't seem to be a Tomcat
bug, but would be good to have a fix for, I've added possible
workarounds for that (reflection code which sets as many static fields
as possible to null in loaded classes when stopping the classloader)
in the latest Tomcat code (which you need to get from SVN). It would
need testing.

--
xxxxxxxxxxxxxxxxxxxxxxxxx
Rémy Maucherat
Developer & Consultant
JBoss Group (Europe) SàRL
xxxxxxxxxxxxxxxxxxxxxxxxx

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


Re: Singleton memory leak after redeploying.

Posted by Lionel Farbos <li...@free.fr>.
On Tue, 29 Nov 2005 00:39:51 -0600
erh@swapsimple.com wrote:

> On Mon, Nov 28, 2005 at 06:24:52PM +0100, Lionel Farbos wrote:
> > 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...
> 
> 	but aren't those supposed to go away when the classloader is no longer
> referenced?  If you can't get to the classloader that loaded the class
> then you can't get to the static fields, so it should be gc'd.  no?
> 
> eric
> 
Sorry, I made a mistake in my tests/analysis : 

the singleton is freed when singleton=null;

If its classloaders is not garbaged on the next "Full GC"
this is due to another object always pointed ...
So, see this : http://opensource2.atlassian.com/confluence/spring/pages/viewpage.action?pageId=2669

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


Re: Singleton memory leak after redeploying.

Posted by er...@swapsimple.com.
On Mon, Nov 28, 2005 at 06:24:52PM +0100, Lionel Farbos wrote:
> 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...

	but aren't those supposed to go away when the classloader is no longer
referenced?  If you can't get to the classloader that loaded the class
then you can't get to the static fields, so it should be gc'd.  no?

eric

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


Re: Singleton memory leak after redeploying.

Posted by Lionel Farbos <li...@free.fr>.
On Tue, 29 Nov 2005 09:31:28 +0100
Mikolaj Rydzewski <mi...@becomo.com> wrote:

> Lionel Farbos wrote:
> 
> >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
> >  
> >
> Does log4j have some nice method to free all Logger objects? It seems 
> like a nightmare to free Loggers from all of my classes by hand...

I think it's not a good idea to have a lot of logger objects in a webapp :
In a webapp, you need only one logger.

Then, on the last servlet destroy, you can freed your logger with logger=null;
I think no other method is needed.

Regards.

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


Re: Singleton memory leak after redeploying.

Posted by Mikolaj Rydzewski <mi...@becomo.com>.
Lionel Farbos wrote:

>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
>  
>
Does log4j have some nice method to free all Logger objects? It seems 
like a nightmare to free Loggers from all of my classes by hand...

-- 
Mikolaj Rydzewski  <mi...@becomo.com>
Becomo S.A.
tel. (12) 2927104



Re: Singleton memory leak after redeploying.

Posted by Lionel Farbos <li...@free.fr>.
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 <ku...@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