You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Thomas �hlen <th...@obidobi.net> on 2002/02/15 18:33:30 UTC

Need to remove reference to the RuntimeSingelton!

I am using Velocity in my Servlet based application currently running on
Tomcat 4.0.2.
Tomcat has a manager with which I can reload my application. To be able to
GC every Singelton class they must have like a close, or destroy method. I
can't find how to do that with the Velocity singelton.

>From what I can tell from the code there is no way to unreference the
internal RuntimeInstance. So each time I reload my application the old
Velocity instances are still lurking in the memory unable to be GC'd.
I have run JProbe on my application and the only classes left after a Reload
and a Garbage Collection is the Velocity instances.

E.g. I need something like this

public class MyServlet
{
    public void init( ServletConfig config )
        throws ServletException
    {
        Velocity.init();
    }

    public void destroy()
    {
        Velocity.destroy();
    }
}

// So from looking at the code this is what I would like to have:

in Velocity

public static destroy()
{
    RuntimeSingelton.destroy();
}

in RuntimeSingelton

public static destroy()
{
    ri = null;
}

Then the Velocity instances should be GC'd on a reload. Well this is how I
have written my packages using Singeltons.

Thanks
Thomas




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/16/02 9:36 AM, "Sylwester Lachiewicz" <ma...@plusnet.pl> wrote:

> i looked into RuntimeInstance and VelocimacroFactory and i found this:
> 
> RuntimeInstance  ref to VelocimactoFactory
> VelocimactoFactory back ref to RuntimeInstance
> 
> RuntimeInstance   ref to VelocimacroManager
> VelocimacroManager back ref to  RuntimeInstance
> 
> when we not remove references to RuntimeInstance - this object will still
> exist in memory
> maybe this is reason why we get memory leak when we reload Velocity
> apllication
> 
> i suggest to add some code to destroy() method to set RuntimeInstance = null
> (and maybe more code) as Thomas wroted


Yep - I am working on this today - I think we should get it in before the
start of the 1.3 release process.

Geir


-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Java : the speed of Smalltalk with the simple elegance of C++... 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Sylwester Lachiewicz <ma...@plusnet.pl>.
i looked into RuntimeInstance and VelocimacroFactory and i found this:

RuntimeInstance  ref to VelocimactoFactory
VelocimactoFactory back ref to RuntimeInstance

RuntimeInstance   ref to VelocimacroManager
VelocimacroManager back ref to  RuntimeInstance

when we not remove references to RuntimeInstance - this object will still
exist in memory
maybe this is reason why we get memory leak when we reload Velocity
apllication

i suggest to add some code to destroy() method to set RuntimeInstance = null
(and maybe more code) as Thomas wroted

thnks
Sylwester

--------
public class RuntimeInstance implements RuntimeConstants, RuntimeServices
{
    /**
     *  VelocimacroFactory object to manage VMs
     */
    private  VelocimacroFactory vmFactory = null;
[..]
 public RuntimeInstance()
    {
        /*
         *  create a VM factory, resource manager
         *  and introspector
         */

        vmFactory = new VelocimacroFactory( this );
                                                                    ^^^^
[..]
}
}

public class VelocimacroFactory
{
    /**
     *  runtime services for this instance
     */
    private RuntimeServices rsvc = null;
[..]

    public VelocimacroFactory( RuntimeServices rs )
    {
        this.rsvc = rs;
                ^^^^^^^^^^^
        /*
         *  we always access in a synchronized(), so we
         *  can use an unsynchronized hashmap
         */
        libModMap = new HashMap();
        vmManager = new VelocimacroManager( rsvc );
                                                      ^^^^^^^^^^^^^^^^
    }
}

public class VelocimacroManager
{
    private RuntimeServices rsvc = null;
    [..]
    VelocimacroManager(RuntimeServices rs)
    {
        this.rsvc = rs;
            ^^^^^^^^^
[..]
    }
}



----- Original Message -----
From: "Thomas Ĺhlen" <th...@obidobi.net>
To: "velocity-user" <ve...@jakarta.apache.org>
Sent: Friday, February 15, 2002 6:33 PM
Subject: Need to remove reference to the RuntimeSingelton!


> I am using Velocity in my Servlet based application currently running on
> Tomcat 4.0.2.
> Tomcat has a manager with which I can reload my application. To be able to
> GC every Singelton class they must have like a close, or destroy method. I
> can't find how to do that with the Velocity singelton.
>
> From what I can tell from the code there is no way to unreference the
> internal RuntimeInstance. So each time I reload my application the old
> Velocity instances are still lurking in the memory unable to be GC'd.
> I have run JProbe on my application and the only classes left after a
Reload
> and a Garbage Collection is the Velocity instances.
>
> E.g. I need something like this
>
> public class MyServlet
> {
>     public void init( ServletConfig config )
>         throws ServletException
>     {
>         Velocity.init();
>     }
>
>     public void destroy()
>     {
>         Velocity.destroy();
>     }
> }
>
> // So from looking at the code this is what I would like to have:
>
> in Velocity
>
> public static destroy()
> {
>     RuntimeSingelton.destroy();
> }
>
> in RuntimeSingelton
>
> public static destroy()
> {
>     ri = null;
> }
>
> Then the Velocity instances should be GC'd on a reload. Well this is how I
> have written my packages using Singeltons.
>
> Thanks
> Thomas



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
>So you keep the velocity.jar in the WEB-INF/lib, or up in tomcat land?

in WEB-INF/lib

Thomas




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/15/02 2:01 PM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> 
>>> I am using Velocity in my Servlet based application currently running on
>>> Tomcat 4.0.2.
>>> Tomcat has a manager with which I can reload my application. To be able
> to
>>> GC every Singelton class they must have like a close, or destroy method.
> I
>>> can't find how to do that with the Velocity singelton.
>> Is that true?  I didn't know that about GC.  Or are you saying so we can
>> unhook the RI?
> 
> Yes exactly. Since Tomcat creates a new classloader when reloading, a new
> Velocity instance will be created on each Velocity.init(). But since the
> singelton in the previous classloader still has an internal reference to
> itself it can't be GC'd. I'm no expert :) but that it my conclusion and it
> seems right.
> 
>> Ya, sure.  This makes sense.  If I do this, can you verify?
> 
> I solved it by writing my own singelton wrapping a VelocityEngine instance.
> But guess it would be nice to have a destroy capable Velocity instance to.

So you keep the velocity.jar in the WEB-INF/lib, or up in tomcat land?

-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
>> I am using Velocity in my Servlet based application currently running on
>> Tomcat 4.0.2.
>> Tomcat has a manager with which I can reload my application. To be able
to
>> GC every Singelton class they must have like a close, or destroy method.
I
>> can't find how to do that with the Velocity singelton.
>Is that true?  I didn't know that about GC.  Or are you saying so we can
>unhook the RI?

Yes exactly. Since Tomcat creates a new classloader when reloading, a new
Velocity instance will be created on each Velocity.init(). But since the
singelton in the previous classloader still has an internal reference to
itself it can't be GC'd. I'm no expert :) but that it my conclusion and it
seems right.

>Ya, sure.  This makes sense.  If I do this, can you verify?

I solved it by writing my own singelton wrapping a VelocityEngine instance.
But guess it would be nice to have a destroy capable Velocity instance to.

Thomas




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/15/02 12:33 PM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> I am using Velocity in my Servlet based application currently running on
> Tomcat 4.0.2.
> Tomcat has a manager with which I can reload my application. To be able to
> GC every Singelton class they must have like a close, or destroy method. I
> can't find how to do that with the Velocity singelton.

Is that true?  I didn't know that about GC.  Or are you saying so we can
unhook the RI?

> 
> From what I can tell from the code there is no way to unreference the
> internal RuntimeInstance. So each time I reload my application the old
> Velocity instances are still lurking in the memory unable to be GC'd.
> I have run JProbe on my application and the only classes left after a Reload
> and a Garbage Collection is the Velocity instances.
> 
> E.g. I need something like this
> 
> public class MyServlet
> {
>   public void init( ServletConfig config )
>       throws ServletException
>   {
>       Velocity.init();
>   }
> 
>   public void destroy()
>   {
>       Velocity.destroy();
>   }
> }
> 
> // So from looking at the code this is what I would like to have:
> 
> in Velocity
> 
> public static destroy()
> {
>   RuntimeSingelton.destroy();
> }
> 
> in RuntimeSingelton
> 
> public static destroy()
> {
>   ri = null;
> }
> 
> Then the Velocity instances should be GC'd on a reload. Well this is how I
> have written my packages using Singeltons.


Ya, sure.  This makes sense.  If I do this, can you verify?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
>This is a big deal, but for reasons outlined earlier in the thread,
>we can't be hasty in how we deal with this because of the potential
>for *huge* problems elsewhere.

Agreed. I will do a simple test of Tomcat's reload with JProbe and take some
screenshots.

Thomas





--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 11:43 AM, "Brad Cox" <bc...@virtualschool.edu> wrote:

> At 11:36 AM -0500 2/18/02, Geir Magnusson Jr. wrote:
>> More standards compliant?  Tomcat is the reference implementation....
> 
> Which tomcat? 3.x or 4.x? Remember, that's when I departed the tomcat camp...

Both.  3.x was the 2.1 spec RI, and 4.x is the 2.2 spec RI.

> 
> I
>> would be interested to know where Jetty adheres to the standard better than
>> tomcat...
> 
> As I said, that's an impression from someone who doesn't use tomcat
> anymore. Perhaps this gc question will produce an example.
> 
> PS I neglected to mention other jetty advantages like speed, ease of
> configuration, etc. And no, I've not done a formal comparison.

And a lousy license :)

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
The bytecodes are language independent. - Sam Ruby



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Brad Cox <bc...@virtualschool.edu>.
At 11:36 AM -0500 2/18/02, Geir Magnusson Jr. wrote:
>More standards compliant?  Tomcat is the reference implementation....

Which tomcat? 3.x or 4.x? Remember, that's when I departed the tomcat camp...

I
>would be interested to know where Jetty adheres to the standard better than
>tomcat...

As I said, that's an impression from someone who doesn't use tomcat 
anymore. Perhaps this gc question will produce an example.

PS I neglected to mention other jetty advantages like speed, ease of 
configuration, etc. And no, I've not done a formal comparison.
-- 
Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
o For industrial age goods there were checks and credit cards.
    For everything else there is http://virtualschool.edu/mybank
o Java Interactive Learning Environment http://virtualschool.edu/jile
o Java Web Application Architecture: http://virtualschool.edu/jwaa



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 11:31 AM, "Brad Cox" <bc...@virtualschool.edu> wrote:

> At 4:54 PM +0100 2/18/02, Thomas ‰hlen wrote:
>> Here we have it Black on White.
>> 
>> http://nyx.obidobi.net/~thomas/tomcat/index.html
>> 
>> Singeltons can't be GC'd after a application reload running Tomcat. My guess
>> is that there are still references to the old WebappClassLoader instances
>> within Tomcat. So this might not be a Velocity issue at all ;).
> 
> Thomas, I'd be very interested in the results of trying the same
> thing with Jetty (jetty.mortbay.com from memory). I moved there from
> Tomcat out of frustration and have never looked back.
> 
> General impression is that its more standards-complient and stable
> overall. Could be wrong.

More standards compliant?  Tomcat is the reference implementation....  I
would be interested to know where Jetty adheres to the standard better than
tomcat...

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"We will be judged not by the monuments we build, but by the monuments we
destroy" - Ada Louise Huxtable


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Brad Cox <bc...@virtualschool.edu>.
At 4:54 PM +0100 2/18/02, Thomas ‰hlen wrote:
>Here we have it Black on White.
>
>http://nyx.obidobi.net/~thomas/tomcat/index.html
>
>Singeltons can't be GC'd after a application reload running Tomcat. My guess
>is that there are still references to the old WebappClassLoader instances
>within Tomcat. So this might not be a Velocity issue at all ;).

Thomas, I'd be very interested in the results of trying the same 
thing with Jetty (jetty.mortbay.com from memory). I moved there from 
Tomcat out of frustration and have never looked back.

General impression is that its more standards-complient and stable 
overall. Could be wrong.
-- 
Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
o For industrial age goods there were checks and credit cards.
    For everything else there is http://virtualschool.edu/mybank
o Java Interactive Learning Environment http://virtualschool.edu/jile
o Java Web Application Architecture: http://virtualschool.edu/jwaa



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/19/02 5:06 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> Yes, the GC works fine. I think we have located the problem to Tomcat not
> releasing all references to old ClassLoaders. Geir is looking in to that so
> lets wait and see what he might find :).

I was going to say that Thomas has located the problem :)  I have been
looking a bit in the tomcat code, but what I have looked at is ok.  Maybe
I'll throw it in a debugger .... I sent a message to Craig.  Ultimately I
was hoping to give the tomcat developers a hint...

geir

> 
> Thomas
> 
>> To test whether self-referential singletons can be garbage collected by
> your
>> VM, unpack the attached file, and type "ant" at the command prompt. If the
>> reported memory usage does not grow, then your VM can GC self-referential
>> singletons. If you get an OutOfMemoryError after a while, then it can't.
>> I've tested the Sun HotSpot Client 1.3.0_02 VM on Win32, and it garbage
>> collects a self-ref singleton class with no problems.
>> 
>> The output of the program should demonstrate that on each iteration, a new
>> singleton is allocated, with a new filler byte array through a new class
>> loader.
>> 
>> Cheers,
>>   Attila.
>> --
>> Attila Szegedi
>> home: http://www.szegedi.org
>> 
> 
> 
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
The bytecodes are language independent. - Sam Ruby  


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
Yes, the GC works fine. I think we have located the problem to Tomcat not
releasing all references to old ClassLoaders. Geir is looking in to that so
lets wait and see what he might find :).

Thomas

> To test whether self-referential singletons can be garbage collected by
your
> VM, unpack the attached file, and type "ant" at the command prompt. If the
> reported memory usage does not grow, then your VM can GC self-referential
> singletons. If you get an OutOfMemoryError after a while, then it can't.
> I've tested the Sun HotSpot Client 1.3.0_02 VM on Win32, and it garbage
> collects a self-ref singleton class with no problems.
>
> The output of the program should demonstrate that on each iteration, a new
> singleton is allocated, with a new filler byte array through a new class
> loader.
>
> Cheers,
>   Attila.
> --
> Attila Szegedi
> home: http://www.szegedi.org
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Attila Szegedi <sz...@freemail.hu>.
To test whether self-referential singletons can be garbage collected by your
VM, unpack the attached file, and type "ant" at the command prompt. If the
reported memory usage does not grow, then your VM can GC self-referential
singletons. If you get an OutOfMemoryError after a while, then it can't.
I've tested the Sun HotSpot Client 1.3.0_02 VM on Win32, and it garbage
collects a self-ref singleton class with no problems.

The output of the program should demonstrate that on each iteration, a new
singleton is allocated, with a new filler byte array through a new class
loader.

Cheers,
  Attila.
--
Attila Szegedi
home: http://www.szegedi.org


----- Original Message -----
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: 2002. február 18. 17:07
Subject: Re: Need to remove reference to the RuntimeSingelton!


On 2/18/02 10:54 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> Here we have it Black on White.
>
> http://nyx.obidobi.net/~thomas/tomcat/index.html
>
> Singeltons can't be GC'd after a application reload running Tomcat. My
guess
> is that there are still references to the old WebappClassLoader instances
> within Tomcat. So this might not be a Velocity issue at all ;).
>

You are the wind beneath my wings :)

No, being Velocity wasn't involved, it might not be a Velocity issue.

However, this is the classic Singlton-GC problem, isn't it?

I mean, you have a reference to TomcatSingleton as a static class field in
TomcatSingleton, which is exactly the problem that I was told the 1.2/1.3
Gcers can't deal with, as they can't ever  GC the Class object for
TomcatSingleton on a class reload, because of the circular reference.  I
know what Attila said, and I don't undestand why they (GCs) can't figure
this out, but they can't.

I suppose if you call TomcatSingleton.destroy() from the
TomcatServlet.destroy() method, all is well? I bet it is.

Now - given that the WebAppClasssloaders are hanging around after the forced
GC, I wonder if *they* are holding the references to the Class objects and
thus prevent the singleton from being GC'd.  That would explain why the
Velocity singleton, which is *NOT* self-referential, also gets caught...

geir

--
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Java : the speed of Smalltalk with the simple elegance of C++...


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 3:35 PM, "Jim Rudnicki" <jd...@pacbell.net> wrote:

> How about adding a method
> 
> Velocity.uninit()
> 
> All well behaved users of init() would call this when the engine was no
> longer used.  Count calls to init() and only destroy the engine instance
> when the count goes back to zero -- reference counted singleton.  Servlets
> call init() in their init() and uninit() in their destroy().
> 
> This should allow the servlet container to unload the servlet and the
> Velocity engine will be safely destroyed when it can.
> 
> Yeh this is somewhat trying to do what the GC should do for us, but it also
> gives explicit control of the life cycle of the engine for those who
> need/desire.
> 

Thomas searched deeper into the tomcat code, and appears to have found the
problem - he posted to tomcat-dev with his suggestion, and Remy seems to
agree with him.

Thanks so much to Thomas for the excellent detective work....

So we'll assume that indeed this is tomcat, and won't worry about it in
velocity?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Java : the speed of Smalltalk with the simple elegance of C++... 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Jim Rudnicki <jd...@pacbell.net>.
How about adding a method

Velocity.uninit()

All well behaved users of init() would call this when the engine was no
longer used.  Count calls to init() and only destroy the engine instance
when the count goes back to zero -- reference counted singleton.  Servlets
call init() in their init() and uninit() in their destroy().

This should allow the servlet container to unload the servlet and the
Velocity engine will be safely destroyed when it can.

Yeh this is somewhat trying to do what the GC should do for us, but it also
gives explicit control of the life cycle of the engine for those who
need/desire.

Jim


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 12:14 PM, "David Tonhofer, m-plify S.A." <d....@m-plify.com>
wrote:

> Hello,
> 
> Is this discussion actually about the 'singletons'
> that are being talked about here:
> 
> http://developer.java.sun.com/developer/technicalArticles/Programming/singl
> etons/
> 
> i.e.
> 
> "The Singleton is a useful Design Pattern for allowing only one instance of
> your
> class, but common mistakes can inadvertently allow more than one instance
> to be
> created. In this article, I'll show you how that can happen and how to
> avoid it."
> 
> Inquiring minds want to know.
> 

I've read that, and I don't think that is causing our problem, but if you
think so, please, shout :)

We tend to do statics w/o lazy instantiation :

  public class Foo {
    public static Bar bar = new Bar();
  }

So the VM takes care of this when the Class object for Foo is created. (Or
so I believe)

So we don't have to worry about multiple copies....

There is a good chance we are being affected by the case noted in the
article relevant to the classloader in servlet engines, but this is
something we want to be servlet engine agnostic towards....

geir

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"We will be judged not by the monuments we build, but by the monuments we
destroy" - Ada Louise Huxtable


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "David Tonhofer, m-plify S.A." <d....@m-plify.com>.
Hello,

Is this discussion actually about the 'singletons'
that are being talked about here:

http://developer.java.sun.com/developer/technicalArticles/Programming/singl
etons/

i.e.

"The Singleton is a useful Design Pattern for allowing only one instance of 
your
 class, but common mistakes can inadvertently allow more than one instance 
to be
 created. In this article, I'll show you how that can happen and how to 
avoid it."

Inquiring minds want to know.




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 11:56 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

>> You are the wind beneath my wings :)
> 
> :)
> 
>> No, being Velocity wasn't involved, it might not be a Velocity issue.
>> 
>> However, this is the classic Singlton-GC problem, isn't it?
> 
> Yes I guess so. Just had to learn it the hard way as always ;).

Don't we all.  'Expert' is a person who already made the mistakes...

> 
>> I suppose if you call TomcatSingleton.destroy() from the
>> TomcatServlet.destroy() method, all is well? I bet it is.
> 
> Yes. That is the way I do it now. Since I used to do Velocity.init() in my
> application I guess there might be lots of other people that do it to. If
> using Tomcats manager servlet this usage will lead to a Out of Memory error
> and require a manual restart of Tomcat sooner or later. As I said before
> beeing able to use a VelocityEngine instance instead made my day, and solved
> the problem.
> 
> Now I just have to solve the same singelton problem I have with Log4J.
> 
>> Now - given that the WebAppClasssloaders are hanging around after the
> forced
>> GC, I wonder if *they* are holding the references to the Class objects and
>> thus prevent the singleton from being GC'd.  That would explain why the
>> Velocity singleton, which is *NOT* self-referential, also gets caught...
> 
> Some input from a Tomcat developer would be nice. They must be aware of this
> problem?

I sent a note to craig, but want to get our ducks in a row before going to
the list.  Maybe if we have a hint for them it would help.

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
POC lives!


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
>You are the wind beneath my wings :)

:)

>No, being Velocity wasn't involved, it might not be a Velocity issue.
>
>However, this is the classic Singlton-GC problem, isn't it?

Yes I guess so. Just had to learn it the hard way as always ;).

>I suppose if you call TomcatSingleton.destroy() from the
>TomcatServlet.destroy() method, all is well? I bet it is.

Yes. That is the way I do it now. Since I used to do Velocity.init() in my
application I guess there might be lots of other people that do it to. If
using Tomcats manager servlet this usage will lead to a Out of Memory error
and require a manual restart of Tomcat sooner or later. As I said before
beeing able to use a VelocityEngine instance instead made my day, and solved
the problem.

Now I just have to solve the same singelton problem I have with Log4J.

>Now - given that the WebAppClasssloaders are hanging around after the
forced
>GC, I wonder if *they* are holding the references to the Class objects and
>thus prevent the singleton from being GC'd.  That would explain why the
>Velocity singleton, which is *NOT* self-referential, also gets caught...

Some input from a Tomcat developer would be nice. They must be aware of this
problem?

Thomas




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 10:54 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> Here we have it Black on White.
> 
> http://nyx.obidobi.net/~thomas/tomcat/index.html
> 
> Singeltons can't be GC'd after a application reload running Tomcat. My guess
> is that there are still references to the old WebappClassLoader instances
> within Tomcat. So this might not be a Velocity issue at all ;).
> 

You are the wind beneath my wings :)

No, being Velocity wasn't involved, it might not be a Velocity issue.

However, this is the classic Singlton-GC problem, isn't it?

I mean, you have a reference to TomcatSingleton as a static class field in
TomcatSingleton, which is exactly the problem that I was told the 1.2/1.3
Gcers can't deal with, as they can't ever  GC the Class object for
TomcatSingleton on a class reload, because of the circular reference.  I
know what Attila said, and I don't undestand why they (GCs) can't figure
this out, but they can't.

I suppose if you call TomcatSingleton.destroy() from the
TomcatServlet.destroy() method, all is well? I bet it is.

Now - given that the WebAppClasssloaders are hanging around after the forced
GC, I wonder if *they* are holding the references to the Class objects and
thus prevent the singleton from being GC'd.  That would explain why the
Velocity singleton, which is *NOT* self-referential, also gets caught...

geir

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Java : the speed of Smalltalk with the simple elegance of C++... 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
Here we have it Black on White.

http://nyx.obidobi.net/~thomas/tomcat/index.html

Singeltons can't be GC'd after a application reload running Tomcat. My guess
is that there are still references to the old WebappClassLoader instances
within Tomcat. So this might not be a Velocity issue at all ;).

Thomas



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 8:45 AM, "Attila Szegedi" <sz...@freemail.hu> wrote:

> You caught me... (partially). I had the VM spec in mind, but it actually does
> not say anything regarding the GC behavior. But, then it's still true that "A
> spec-conforming VM can safely unload this class if it and its class loader are
> not accessible from any live thread. " - this behavior really does not
> contradict anything in the VM spec, and Sun and IBM VMs certainly work this
> way. On a sidenote, with a Sun VM you can actually use the -Xnoclassgc switch
> to disable garbage collection of unreachable classes.
> 
> But you're right it's not explicitly stated in any spec.

I wasn't trying to catch you :)

I was trying to learn something.  I went through the VM spec a bit, and
found out that it punted the whole thing to the implementer, but didn't see
anything beyond that.  Also, Craig said something to me along the same
lines, how the pre 1.2 GC's had some weird problems where they would
actually arbitrarily GC singletons underneath you.

I understand why they do it this way (leaving it up to the implementer), but
hard to swallow for something that's "Write once..."

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Attila Szegedi <sz...@freemail.hu>.
You caught me... (partially). I had the VM spec in mind, but it actually does not say anything regarding the GC behavior. But, then it's still true that "A spec-conforming VM can safely unload this class if it and its class loader are not accessible from any live thread. " - this behavior really does not contradict anything in the VM spec, and Sun and IBM VMs certainly work this way. On a sidenote, with a Sun VM you can actually use the -Xnoclassgc switch to disable garbage collection of unreachable classes.

But you're right it's not explicitly stated in any spec.
Cheers,
  Attila.

----- Original Message ----- 
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: 2002. február 18. 14:18
Subject: Re: Need to remove reference to the RuntimeSingelton!


<snip/>

> Well, my conclusion is the same as Attila's, but I am wondering what spec
> Attila is referring to :)


<snip/>

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
POC lives!


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>




Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 7:09 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

>> Not true. A spec-conforming VM can safely unload this class if it and its
> class loader are not
>> accessible from any live thread.
>> 
>> GC is not confused with circular references of any kind - that's why it is
> superior to reference
>> counting methods. Circular structures will be GCed if they're (as said
> above) inaccessible for
>> all live threads.
>> 
>> Cheers,
>>  Attila.
> 
> Well if this is the case then the problem with garbage collection of
> Singeltons must be caused by Tomcat not releasing reference to old
> classloaders and therefore can't delete instances assigned to static
> variables within those classloaders. But what do I know ;). For now I
> manually "null" the static variables and have consequently removed the
> memory leak I had when reloading with the Tomcat manager.
> 

Well, my conclusion is the same as Attila's, but I am wondering what spec
Attila is referring to :)

It could be tomcat, but I don't understand how quite yet.  I could be a bug
in the VM, but I also doubt that.

This is a big deal, but for reasons outlined earlier in the thread, we can't
be hasty in how we deal with this because of the potential for *huge*
problems elsewhere.

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
POC lives!


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
>Not true. A spec-conforming VM can safely unload this class if it and its
class loader are not
>accessible from any live thread.
>
>GC is not confused with circular references of any kind - that's why it is
superior to reference
>counting methods. Circular structures will be GCed if they're (as said
above) inaccessible for
>all live threads.
>
>Cheers,
>  Attila.

Well if this is the case then the problem with garbage collection of
Singeltons must be caused by Tomcat not releasing reference to old
classloaders and therefore can't delete instances assigned to static
variables within those classloaders. But what do I know ;). For now I
manually "null" the static variables and have consequently removed the
memory leak I had when reloading with the Tomcat manager.

Thomas



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Attila Szegedi <sz...@freemail.hu>.
Not true. A spec-conforming VM can safely unload this class if it and its class loader are not accessible from any live thread. 

GC is not confused with circular references of any kind - that's why it is superior to reference counting methods. Circular structures will be GCed if they're (as said above) inaccessible for all live threads.

Cheers,
  Attila.

--
Attila Szegedi
home: http://www.szegedi.org

----- Original Message ----- 
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: 2002. február 17. 22:40
Subject: Re: Need to remove reference to the RuntimeSingelton!

<snip/>


I understand the 'classic' singleton GC problem with the following :

 public class Foo {
  public static Foo foo = new Foo()
 }

Where this kind of singleton presents problems to the GC, because it can't
ever GC the Class object for the class Foo, because of the static reference
to itself via the instance of the class (  the new Foo() ).  Therefore it
can't GC the instance of Foo().

This I get.

<snip/>

geir

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>




Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/18/02 9:15 PM, "Bill Burton" <bi...@progress.com> wrote:

> Hello,
> 
> "Geir Magnusson Jr." wrote:
>> 
>> On 2/17/02 6:12 PM, "Thomas Åhlen" <th...@obidobi.net> wrote:
>>> Yes if you have one Velocity instance for several applications this would
>>> not work. I guess that the best practice would be not to use the Singelton
>>> in a reloadable application and instead wrapp a VelocityEngine instance.
>> 
>> Bingo!
>> 
>> After 1.3, we'll add a new Servlet that's based on the engine, not the
>> singleton.  Of course the downside is that you don't get sharing across
>> servlets in the same webapp.
> 
> Why not modify VelocityServlet to use an attribute in the ServletContext
> to save a reference to a VelocityEngine instance?  If when initializing,
> it finds the SevletContext attribute and it is non null or references an
> instanceof VelocityEngine, then it would just use that reference.

You'd have to find a way to do that atomically, of course :)  I don't know
if you could synchronize on the ServletContext itself....

> Otherwise it would create a new VelocityEngine instance and save that to
> the ServletContext.  So you would get the same behavior as now but without
> using statics to support it.  Also, it shouldn't be necessary to null out
> the reference to VelocityEngine in a Servlet destroy() and it shouldn't be
> done unless it can be ensured there are no other servlets referencing it
> for the reasons Geir points out.

Si'
 
> Maybe there could be a configuration option so if someone wanted a
> separate VelocityEngine instance per servlet it could be supported.  One
> way would be to allow specifying the key to use for the ServletContext
> attribute name as described above.  Then it would also be possible to
> support multiple shared VelocityEngine instances within the
> ServletContext.

I think this will turn out to be a tomcat 4 problem, and that our current
use of singleton will be ok.  However, having a VelocityEngine based sevlet
might be nice - I think we just want to avoid making this too
'framework-ish'.  In all, I think the VelocityServlet is a useful building
block (as will one based on the VelocityEngine) but I think we need to
carefully consider adding stuff that hints of multi-servlet structure, as
you can use Turbine et al for that...


-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
POC lives!


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Bill Burton <bi...@progress.com>.
Hello,

"Geir Magnusson Jr." wrote:
> 
> On 2/17/02 6:12 PM, "Thomas Åhlen" <th...@obidobi.net> wrote:
> > Yes if you have one Velocity instance for several applications this would
> > not work. I guess that the best practice would be not to use the Singelton
> > in a reloadable application and instead wrapp a VelocityEngine instance.
> 
> Bingo!
> 
> After 1.3, we'll add a new Servlet that's based on the engine, not the
> singleton.  Of course the downside is that you don't get sharing across
> servlets in the same webapp.

Why not modify VelocityServlet to use an attribute in the ServletContext
to save a reference to a VelocityEngine instance?  If when initializing,
it finds the SevletContext attribute and it is non null or references an
instanceof VelocityEngine, then it would just use that reference. 
Otherwise it would create a new VelocityEngine instance and save that to
the ServletContext.  So you would get the same behavior as now but without
using statics to support it.  Also, it shouldn't be necessary to null out
the reference to VelocityEngine in a Servlet destroy() and it shouldn't be
done unless it can be ensured there are no other servlets referencing it
for the reasons Geir points out.

Maybe there could be a configuration option so if someone wanted a
separate VelocityEngine instance per servlet it could be supported.  One
way would be to allow specifying the key to use for the ServletContext
attribute name as described above.  Then it would also be possible to
support multiple shared VelocityEngine instances within the
ServletContext.

-Bill

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/17/02 6:12 PM, "Thomas Åhlen" <th...@obidobi.net> wrote:

>> Now, I don't understand why this would apply to the Velocity singleton
>> in the VelocityServlet, because that isn't self-referential.  It's not
> really
>> a singleton either, as it's just a static wrapper to the RuntimeSingleton
>> class.
> 
> Well I'm am not using the VelocityServlet. I was using my own servlet in
> which I used to initialize Velocity with Velocity.init(). But each time I
> reload my application the memory usage just kept increasing. Since I wasn't
> sure what was causing it so I downloaded JProbe ServerSide Edition and ran
> the Profiler and Memory debugger (only a 7 days trial version i might add,
> what open source programmer can afford $2000 :/ ).

Not this one, certainly - that's why I was hoping you could help :)  If they
have a linux version, I'll try that myself.  I am betting they don't have an
OS X version :)

>  Ontop of it I ran Tomcat
> 4.0.2 and my application. Looking at memory usage and what classes existed
> in memory I could see that after each reload I got new instances of all used
> velocity classes and the old and unused ones where still present in the
> memory and they could not be GC'ed. I had the same problem with one
> Singelton of my own. By adding a destroy method that unreferensed the static
> variable it could be GC'd.

I believe you - however, when you say it could not be GC'd, did it say that?
Or is that inference? (Just curious how much Jprobe figures out...)
 
>> This is good, but there are going to be some snags.  First, there is no
>> guarantee that it's safe to call a destroy() on the Velocity class when the
>> servlet gets destroyed, as there may be other classes that are using it.
>> For example, other servlets could be.
> 
> Yes I see your point. But can servlets be destroyed in any other way than
> doing a reload?
> I am not sure how Tomcat works so I only do logical assumptions.

Sure - I guess. But you can imagine all sorts of bizarre situations that
people can concoct...  The real key here is that we would be rendering a
singleton that isn't devoted only to the Servlet unusable because of a
lifecycle in the servlet container.  If we were using Velocity somewhere
else, such as in a Filter, I would bet that would take a while to debug :)
 
>> Also, you might argue that's ok because the servlets are all getting
>> destroyed when the webapp classloader cycles, but you don't know ahead of
>> time if the Velocity class is from that classloader.  It could be from the
>> parent classloader right?
> 
> Yes if you have one Velocity instance for several applications this would
> not work. I guess that the best practice would be not to use the Singelton
> in a reloadable application and instead wrapp a VelocityEngine instance.

Bingo!

After 1.3, we'll add a new Servlet that's based on the engine, not the
singleton.  Of course the downside is that you don't get sharing across
servlets in the same webapp.

> 
>> So it's possible that the above puts the Velocity class into a broken
> state,
>> because the RuntimeSingleton is now w/o an instance.
> 
> Yes you are right.
> 
> The way I solved it was to create my own Singelton that I know are created
> in the classloader of my application. Something like this...
> 
> public class VelocityParser
> {
>   static VelocityEngine parser = new VelocityEngine();
> 
>   public static void init()
>   {
>       parser.init();
>   }
> 
>   public static void destroy()
>   {
>       parser = null;
>   }
> }
> 
> In my servlet I call
> 
> public class MyServlet
> {
> 
>   public void init( ServletConfig config )
>       throws ServletException
>   {
>      VelocityParser.init();
>   }
> 
> 
>   public void destroy()
>   {
>       VelocityParser.destroy();
>   }
> }
> 
> This solved the problem and all Velocity instances can be GC'd on a reload.
> 
> Hope I made some sense ;)

Yep.  You made sense the first time.  We'd switch to that in a second if I
was sure that people didn't access the Velocity wrapper in their derived
classes.

Can you do a test - my minimal knowledge of things is such that the above
would get GC'd even if you didn't null the reference in
VelocityParser.destroy() because there is no self reference.  Can you try
that?  Just comment out 'parser = null' and retry, seeing if it "can't" get
GC'd?

Thanks for all the help so far.

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
My inner cowboy needs to yodel.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by Thomas �hlen <th...@obidobi.net>.
>Now, I don't understand why this would apply to the Velocity singleton
>in the VelocityServlet, because that isn't self-referential.  It's not
really
>a singleton either, as it's just a static wrapper to the RuntimeSingleton
>class.

Well I'm am not using the VelocityServlet. I was using my own servlet in
which I used to initialize Velocity with Velocity.init(). But each time I
reload my application the memory usage just kept increasing. Since I wasn't
sure what was causing it so I downloaded JProbe ServerSide Edition and ran
the Profiler and Memory debugger (only a 7 days trial version i might add,
what open source programmer can afford $2000 :/ ).  Ontop of it I ran Tomcat
4.0.2 and my application. Looking at memory usage and what classes existed
in memory I could see that after each reload I got new instances of all used
velocity classes and the old and unused ones where still present in the
memory and they could not be GC'ed. I had the same problem with one
Singelton of my own. By adding a destroy method that unreferensed the static
variable it could be GC'd.

>This is good, but there are going to be some snags.  First, there is no
>guarantee that it's safe to call a destroy() on the Velocity class when the
>servlet gets destroyed, as there may be other classes that are using it.
>For example, other servlets could be.

Yes I see your point. But can servlets be destroyed in any other way than
doing a reload?
I am not sure how Tomcat works so I only do logical assumptions.

>Also, you might argue that's ok because the servlets are all getting
>destroyed when the webapp classloader cycles, but you don't know ahead of
>time if the Velocity class is from that classloader.  It could be from the
>parent classloader right?

Yes if you have one Velocity instance for several applications this would
not work. I guess that the best practice would be not to use the Singelton
in a reloadable application and instead wrapp a VelocityEngine instance.

>So it's possible that the above puts the Velocity class into a broken
state,
>because the RuntimeSingleton is now w/o an instance.

Yes you are right.

The way I solved it was to create my own Singelton that I know are created
in the classloader of my application. Something like this...

public class VelocityParser
{
    static VelocityEngine parser = new VelocityEngine();

    public static void init()
    {
        parser.init();
    }

    public static void destroy()
    {
        parser = null;
    }
}

In my servlet I call

public class MyServlet
{

    public void init( ServletConfig config )
        throws ServletException
    {
       VelocityParser.init();
    }


    public void destroy()
    {
        VelocityParser.destroy();
    }
}

This solved the problem and all Velocity instances can be GC'd on a reload.

Hope I made some sense ;)

Thomas




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Need to remove reference to the RuntimeSingelton!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 2/15/02 12:33 PM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> I am using Velocity in my Servlet based application currently running on
> Tomcat 4.0.2.
> Tomcat has a manager with which I can reload my application. To be able to
> GC every Singelton class they must have like a close, or destroy method. I
> can't find how to do that with the Velocity singelton.
> 
> From what I can tell from the code there is no way to unreference the
> internal RuntimeInstance. So each time I reload my application the old
> Velocity instances are still lurking in the memory unable to be GC'd.
> I have run JProbe on my application and the only classes left after a Reload
> and a Garbage Collection is the Velocity instances.

I have been thinking about this, and I don't understand why it's a problem.

The following should be read with the understanding that I don't know much
about GC.

I understand the 'classic' singleton GC problem with the following :

 public class Foo {
  public static Foo foo = new Foo()
 }

Where this kind of singleton presents problems to the GC, because it can't
ever GC the Class object for the class Foo, because of the static reference
to itself via the instance of the class (  the new Foo() ).  Therefore it
can't GC the instance of Foo().

This I get.

Now, I don't understand why this would apply to the Velocity singleton in
the VelocityServlet, because that isn't self-referential.  It's not really a
singleton either, as it's just a static wrapper to the RuntimeSingleton
class. 

So what would help is if you could be a little more precise - what exactly
is getting left behind? I am about to start doing this w/o the benefit of
Jprobe, so if you can save me some time, that would be great. :)


> 
> E.g. I need something like this
> 
> public class MyServlet
> {
>   public void init( ServletConfig config )
>       throws ServletException
>   {
>       Velocity.init();
>   }
> 
>   public void destroy()
>   {
>       Velocity.destroy();
>   }
> }
> 
> // So from looking at the code this is what I would like to have:
> 
> in Velocity
> 
> public static destroy()
> {
>   RuntimeSingelton.destroy();
> }
> 
> in RuntimeSingelton
> 
> public static destroy()
> {
>   ri = null;
> }
> 
> Then the Velocity instances should be GC'd on a reload. Well this is how I
> have written my packages using Singeltons.

This is good, but there are going to be some snags.  First, there is no
guarantee that it's safe to call a destroy() on the Velocity class when the
servlet gets destroyed, as there may be other classes that are using it.
For example, other servlets could be.

Also, you might argue that's ok because the servlets are all getting
destroyed when the webapp classloader cycles, but you don't know ahead of
time if the Velocity class is from that classloader.  It could be from the
parent classloader right?

So it's possible that the above puts the Velocity class into a broken state,
because the RuntimeSingleton is now w/o an instance.

The right way is to figure out what exactly is happening, and make it so the
GC can reclaim.  Doing some research showed me that this is hard - because
the implementation of the GC is left up to the implementer of the VM - the
VM spec says nothing on what it's supposed to do...

geir

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>