Amir Pashazadeh
Amir Pashazadeh

Reputation: 7302

Do constant static fields cause memory leak in an web application?

I know that static field can cause a memory leak, because they will not be GCed.

But when there is an web application which is deployed in a container (such as Tomcat), each application has its own ClassLoader, and it can be undeployed.

My question is, do garbage collector claims objects referenced by static members of the classes which are going to be unloaded?

The simplest case is a singleton (implemented by an static variable referencing self), will it be GCed if the application is undeployed?

Upvotes: 1

Views: 1146

Answers (1)

Sachin Thapa
Sachin Thapa

Reputation: 3709

This might answer your question:

When an app is stopped, Tomcat (even before 6.0.24) nullifies the value of all static class variables of classes loaded by the WebAppClassLoader. In some cases, it may fix a classloader leak (for example because of a custom ThreadLocal class, see above), but even if we still have a leak, it may decrease the amount of memory lost

You can read more here

Cheers !!

Upvotes: 2

Related Questions