The implication that the example singleton class causes a memory leak just because "...it creates a hard reference to a class instance into the class itself" is hard to believe. There are plenty of other ways to pin classes loaded by the web app's class loader, but the singleton pattern isn't one of them.

One common way to pin classes is through the JavaBeans Introspector. Since the Introspector caches information about the classes it processes, those references will keep the web app's class loader from becoming eligible for collection. If you're using a container that doesn't take this into account, the common solution is to create a ServletContextListener that calls Introspector.flushCaches in its contextDestroyed method.

If your singleton is really causing a performance issue, there's probably something else going on. If it's released open source, you might want to post a link to the code so someone can take a look at it.

Luca Passani wrote:
Tarek Nabil wrote:

Thanks a lot Luca for pointing this out. I must say it's completely new
information to me. I wonder if it's really accurate and relevant to all
containers.

For example, in Oracle Containers for Java (OC4J), when you redeploy an
application, the OC4J instance is restarted. You can reload an
application without restarting the OC4J instance. Do you think that
would make it immune against this kind of problem? I'm also going to the
Oracle forums with that question.
can you post (or send me off-line) a pointer to your post on the Oracle forum?

I am interested in this for a bit of open source that I have released and where I have used a Singleton. If I restart the application, it takes increasingly more time for the app-server to recover each time the Singleton is rebuilt.
This is a problem I want to fix for the first release.

Luca

--
Kris Schneider <mailto:[EMAIL PROTECTED]>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to