Memory Leak in Singleton Pattern

Singleton Design Pattern is basically used when we want to limit object creation. Singleton Design Pattern is considered one of the most simple design pattern. The main concern of this pattern is to ensure that only one instance of a class is created and provide a global point of access to the object.

 

We can make singleton design pattern by declaring constructor of class as private, so that it prevents instantiate the Singleton class from outside the class. It also provide one public static method to provide the global point of access to the Singleton object and returns the instance to the caller.

There are two ways for creating Singleton Design Pattern

  • Early Instantiation: Here the instance is created at the load time. In such case, we create the instance of the class at the time of declaring the static data member, so instance of the class is created at the time of classloading.
public class SingleObject {

   //create an object of SingleObject
   private static SingleObject instance = new SingleObject();

   //make the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}

   //Get the only object available
   public static SingleObject getInstance(){
      return instance;
   }

   public void showMessage(){
      System.out.println("Android");
   }
}

 

  • Lazy Instantiation: Here the instance is created when required. In such case, we create the instance of the class in synchronized method or synchronized block, so instance of the class is created when required.
public class SingletonExample {
    // Static member holds only one instance of the
    // SingletonExample class

    private static SingletonExample singletonInstance
 
    // SingletonExample prevents any instantiating
    private SingletonExample() {

    }

    // Providing Global point of access
    public static SingletonExample getSingletonInstance() {
        if (null == singletonInstance) {
            singletonInstance = new SingletonExample();
      }
       return singletonInstance
    }
 
    public void message(){
        System.out.println("Android")
   }

}

 

Now lets discuss the main point of this topic. Why Lazy Instantiation of Singleton Pattern is good ? Well, as we saw the early instantiation is created when the class is loaded due to static member. Due to this even if we are not using this object the memory will be allocated and thus memory leak problem will be seen. However in Lazy instantiation we instantiate the object when need, as a result we will allocate the memory only when we need it.

Early instantiation is not recommended as we allocate the memory to resources even if we don’t need it. As we use static member to allocate the memory it will be in the memory throughout the lifecycle of the application until and unless we explicitly assign it to null. It would be dangerous in the real life scenario like using Database connection. Therefore we close the connection of database when it is not use to avoid memory leak.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s