devxlogo

Lazy Versus Eager Instantiation

Lazy Versus Eager Instantiation

There are techniques for creation of objects (read allocation of memory)widely known as lazy Instantiation and Eager Instantiation.
Lazy instantiation is a memory conservation technique, by which, a programdelays the creation of objects until those objects are needed.In Java, there are two categories of lazy instantiation:

1. Lazy class loading
The Java runtime loads classes into memory only when they’re firstreferenced. This may happen due to a first call to a static method of a class as in:

 	aClass.someStaticMethod();

or a first call to the “new” operator to create an instatnce of a class asin:

             AClass aClass = new AClass();

This is a very important feature of the Java runtime. Memory usage can besignificantly reduced. For example, if a part of a program is never run,classes that are only referenced by that part of the program will never beloaded.

2. Lazy object creation
This is tightly coupled to lazy class loading. It refers to when you delaythe creation of an instance of a class until you really need it in the code.

Eager Instantiation, on the other hand, refers to technique that instancesof objects are created before any part of the code actually asks for them.This may considerably improve runtime performance since live objects aresitting in runtime memory waiting to be called. An example of eagerinstantiation is creation of resource pools, e.g. a pool of databaseconnections waiting for client calls.

Suppose you have to load images on request basedcorresponding image file names. You can write code like:

 public class AnImageFile{   private String m_filename;   private Image m_image;   public AnImageFile(String filename)   {       m_filename=filename;       //Now load the image in constructor   }   public String getName(){ m_return filename;}   public Image getImage(){return m_image;}}

This is an eager approach that guarantees the availability of an image rightafter creation of an instance of AnImageFile class. This will boostperformance when a call to getImage(…) is made. But if you have manyimages, and create AnImageFile instances of them, you may use up a lot ofmemory unnecessarily. You can trade performance benefits to memory uasagevia lazy instantiation, as in:

 public class ImageFile{   private String m_filename;   private Image m_image=null;   public ImageFile(String filename)   {       //Here we just store the filename       m_filename=filename;   }   public String getName(){ return m_filename;}   public Image getImage()   {       if(m_image==null)       {           //load the image       }       return m_image;    }}

Here, the actual image is loaded only on the first call to getImage().

So, you can reduce memory usage and startup time by using lazy instantiationand paying the price for performance hit when you create an object when itis actually need. Or, you can increase the startup time, and use up morememory upfront to eagerly instantiate objects in order to boost performance.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist