Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton lazy vs eager instantiation

If a singleton is implemented as follows,

class Singleton {     private static Singleton instance = new Singleton();      public static Singleton getInstance() {         return instance;     } } 

How is this implementation different from the lazy initialization approach? In this case,the instance will be created when the class is loaded and the class itself is loaded only on the first active use (for example, Singleton.getInstance() not when you declare for instance Singleton singleton = null;)

Even with lazy initialization approach, the instance is created on the call to getInstance()

Am i missing something here?

like image 256
java_geek Avatar asked Oct 17 '11 06:10

java_geek


People also ask

What is the main advantage of lazy loading over eager for a singleton?

With lazy initialization you crate instance only when its needed and not when the class is loaded. So you escape the unnecessary object creation.

What is early and lazy instantiation in singleton pattern?

There are two forms of singleton design pattern. Early Instantiation: creation of instance at load time. Lazy Instantiation: creation of instance when required.

Is Singleton lazy initialized?

Singleton Class in Java using Lazy LoadingThe instance could be initialized only when the Singleton Class is used for the first time. Doing so creates the instance of the Singleton class in the JVM Java Virtual Machine during the execution of the method, which gives access to the instance, for the first time.

Why is lazy vs eager initialization preferred?

It's better to use lazy instantiation for expensive objects that you might never use. However, if we are working with an object that we know will be used every time the application is started, and if the object's creation is expensive, in terms of system resources, then it's better to use eager instantiation.


1 Answers

With lazy initialization you crate instance only when its needed and not when the class is loaded. So you escape the unnecessary object creation. That being said there are other things to consider too. In lazy initialization you give a public API to get the instance. In multi-threaded environment it poses challenges to avoid unnecessary object creation. you put synchronization blocks which poses unnecessary locking to be done to check for object already created. So it becomes a performance issue in this case.

So if you are sure that creating you object is not going to take any significant memory and its almost always going to be used in your application then its good to create in static initialization. Also please do not forget to make your instance final in this case as it make sures that the object creation is reflected properly and in totality to main memory which is important in multi-threaded environment.

Please refer this tutorial from IBM on Singleton+ Lazy Loading+ Multithreaded Environment case

===============Edit on 09/09/2018====================

You should also look at object creation on demand pattern here.

like image 181
Saurabh Avatar answered Sep 21 '22 14:09

Saurabh