Precise Java Google
Home  | J2SE  | J2EE  | Book  | Free Books  | Contact
Collections  | StringAndStringBuffer  | Serialization  | IO  | Objects  | Loops  | Exceptions

Performance improvement techniques in Object creation

 

This topic illustrates the performance improvement techniques in object creation with the following sections:

Overview of Object creation

Object contain data and methods to manipulate the data.  Whenever we create an object there is an overhead involved. Now let us look at an example to understand the overall process :

class ObjectOne{
    int a;

    ObjectOne(){
        a=999;
    }
    int square(){
        return a*a;
    }

    String string (String str){
        return "hello"+str;
    }

}

class ObjectTwo extends ObjectOne{
    String name ;

    ObjectTwo(){
        name ="rr";
    }
   
    public static void main(String args[]){
        ObjectTwo t2 = new ObjectTwo();
    }
}

So now when object t2 is created the following steps are involved :

  • Memory is allocated to all the variables
  • All super class variables are also allocated memory
  • All sub class variables, super class variables are initialized .
  • The constructor is invoked.

So whenever we create an object the above steps are repeated which take considerable resources so it is very important to decide whether creating a new object is required or not.

And now let us look at where the objects are placed in memory :

All objects are placed on heap, their address on the heap is stored in the stack. All class  variables are stored in the method area. All primitive data types are stored on the stack.

Note: This section assumes that reader has some basic knowledge of Java.

Optimization techniques in Object creation

  • Avoid creating objects in a loop.
  • Always try to use String literals instead of String objects.

Eg . String str1 = "Hello I am here ";   //String literal

        String str2= "Hello I am here ";   //String literal

         String str3 = new ("Hello I am here ");  //String Object

When we create a String without the new operator and if the content is already existing it uses a single instance of the literal instead of creating a new object every time.

  • Never create objects just for accessing a method.
  • Whenever you are done with an object make that reference null so that it is eligible for garbage collection.
  • Never keep inheriting chains long since it  involves calling all the parent constructors all along the chain until the constructor for java.lang.Object is reached.
  • Use primitive data types rather than using wrapper classes.
  • Whenever possible avoid using class variables, use local  variables since accessing local variables is faster than accessing class variables.
  • Use techniques such as lazy evaluation. Lazy evaluation refers to the technique of avoiding certain computations until they are absolutely necessary. This way we put off certain computations that may never need to be done at all.
  • Another technique is Lazy object creation : i.e. delaying the memory allocation to an object till it is  not being put into use. This way a lot of memory is saved till the object is actually put in to use.

 

Key Points

  1. Avoid creating objects in a loop.
  2. Use String literals instead of String objects (created using the 'new' keyword) if the content is same.
  3. Make used objects eligible for garbage collection.
  4. Do not keep inheritance chains long.
  5. Accessing local variables is faster than accessing class variables
  6. Use lazy evaluation, lazy object creation whenever possible.

 

Feed back

We appreciate and welcome your comments on this section. Email commentsZZZ@precisejavaZZZ.com (remove ZZZ which is placed to prevent spam). Please note that we may not be able to reply to all the emails due to huge number of emails that we receive but we appreciate your comments and feedback.

 





Copyright © 2001-2005, Ravi Kalidindi and Rohini Kalidindi. All rights reserved.