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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS