Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Spring Singleton Scope is Garbage Collected?

I am new to Spring framework. I have been confused about the concept of singleton in Spring and it's garbage collection. I have read many questions and articles to get the answer to my question that how Spring Singleton scope is garbage collected. I only got the answers about prototype scope garbage collection, but the articles regarding singleton scope were not clear to me. Could someone give the details on this issue.

like image 229
Anant666 Avatar asked Jun 03 '16 02:06

Anant666


People also ask

Are singletons garbage collected?

Singleton class has a static reference to the instantiated singleton object and hence it will never be garbage collected unless ofcourse as Jon Skeet as stated that the context that loaded this class (class loader) is itself eligible for garbage collection in which case that static reference will not longer be a GC ...

Are prototype beans garbage collected?

It's because that as long as the prototype bean does not hold a reference to another resource itself, such as a database connection or a session object, it will get garbage collected as soon as all references to the object have been removed or the object goes out of scope.

How is garbage collection performed?

In Java, garbage collection is the process of managing memory, automatically. It finds the unused objects (that are no longer used by the program) and delete or remove them to free up the memory. The garbage collection mechanism uses several GC algorithms. The most popular algorithm that is used is Mark and Sweep.

Why Spring bean scope is singleton by default?

When a bean is a singleton, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned. Only when you have to keep some session details you should use for example session scope.


2 Answers

In Spring, most of the classes you write will be Singletons. This means that there is only ever one instance of these classes created. These classes are created when the Spring container starts and are destroyed when the Spring container stops.

The reason that Spring singleton objects are different from simple Java objects, is that the container maintains a reference to them, and they are able to be used anywhere in your code at any time.

I'll give you an example using the Spring container to illustrate what I mean. This is NOT how you should do this normally when writing a Spring app, this is just an example.

@Component
public class ExampleClass implements ApplicationContextAware {
    /* 
     * The ApplicationContextAware interface is a special interface that allows 
     * a class to hook into Spring's Application Context. It should not be used all
     * over the place, because Spring provides better ways to get at your beans
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MyBean bean = applicationContext.getBean("MyBean");
    }
}

What the above code does is say to Spring "I want the instance of MyBean that you have discovered when the container started" (Classpath Scanning). Spring should have a (proxy) instance of this class already created and available for your use.

From the Spring Documentation

The Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.

Because that bean has been cached inside the application context, it is never eligible for garbage collection until the application context is destroyed.

like image 198
JamesENL Avatar answered Oct 17 '22 02:10

JamesENL


This doesn't seem to be entirely true. We have an enterprise spring application with close to 12000 singlton classes. If the JVM regardless of the application server, is started with around 4GBs of heap the heap fills up in around 3 redeploys or even undeploy and deploy. Even without any activity other then the deployment. The heap dump too, shows exactly 3 copies of the singletons. So it's not actually getting destroyed with application context. We have looked for a solution to this without success as this is a big time waster for the developers. They have to waste a lot of time recycling application server often while debugging or testing. In case of weblogic this happens even when just stopping and starting the application a few times.

like image 2
Quezars Avatar answered Oct 17 '22 01:10

Quezars