Tuesday, 8 December 2015

WebService Client Generation Error with JDK8

java.lang.AssertionError: org.xml.sax.SAXParseException:

Create a file named jaxp.properties (if it doesn't exist) under /path/to/jdk1.8.0/jre/lib and then write this line in it:

javax.xml.accessExternalSchema = all

That's all. Enjoy JDK 8.

Thursday, 20 August 2015

Just 3 Steps to Setting Up a Tomcat Server Log file in Eclipse ?

  • In the servers tab, double-click on the Tomcat Server. You will get a screen called Overview.
  • Click on "Open launch configuration". Click on the "Common" tab.
  • Towards the bottom of the screen you can check the "File" checkbox and then specify a file that can be used to log your console (catalina.out) output.
  • Finally, restart the Tomcat server.

Monday, 3 August 2015

How to add or remove or list certificates from keystore or trustStore in Java ?

                    How to add certificates on keystore in Java is primary questions when you start working on SSL connection and simple answer is keytool utility in Java is used to add or list Certificates into keystore. SSL is industry standard for secure communication between two parties e.g. client and server. SSL offers two benefits, it encrypts data transferred between client and server to make it hard for someone to access and understand in between and SSL also verify identity of two parties in communication and certificates are used for that purpose. SSL Setup in Java comes during various process e.g. Setting up SSL on tomcat, configuring messaging over SSL or JDBC over SSL are some examples of task where you need to deal with keyStore, certificates and trustStores. for those who are not aware of what is a keystore in Java and what is certificates, we will see brief introduction in next section.
Basics of SSL Certificates and Keystore in Java : -
                        When we access a secure site which uses SSL for providing identity and encryption, it provides a certificates which was verified by a trusted third party sites like verisign, godaddy or thwate. by using certificates browser or java clients knows that they talking to the correct site (who it claims to be) and not on redirected proxy site. this step is pretty transparent if you access websites using browser because if certificate is not on browser's trusted store it will ask you to add that certificate and it will be subsequently added, But when you access a secure site using Java program, this step of certificate hand shaking is not transparent to user and certificates are verified form JRE's trustStore. This trustStore is located on JDK Installation directory referred by JAVA_HOME  e.g. JAVA_HOME/jre/lib/security and commonly named as "cacerts".If certificate provided by secure site is present on JRE's trustStore SSL connection would be established but if certificate is not there than Java will throw exception and to solve that you need to add that certiificate into trustStore. keyStore and trustStore is often used interchangeably and same file can act as keystore as well as trustStore it just matter of pointing javax.net.ssl.keyStore and javax.net.ssl.trustStore properties to that file but there is slightly difference between keystore and trustStore. keyStore is used to store individual identity or certificate while trustStore is used to store other parties certificates signed by CA.
How to add ,remove and list certiifcates from Java keystore :-
  • In this article we will see how to add ,remove and list certiifcates from Java keystore using keytool utility.
  • keytool is binary located inside JAVA_HOME/jre/lib/security folder and used for adding, removing and listing certificates. 
 here is step by step example of adding certificates in Java:
Example of listing certificates form Java Keystore :-
                 Before adding new certificates in keystore or truststore its good to see, count and verify already installed certificates. run following keytool command in commonprompt to get a list of certififcates from keystore:
Step 1 :- Open the command prompt, go to the path where the java installed in your machine.
Step 2 :- Run the below command to get the list of certificates available in your cacerts file.
               keytool -list -keystore cacerts
               You see currently keystore "cacerts" holds 77 certificates.



Example of adding Certificate on Java KeyStore :-
Now let's see example of adding certificates into keytstore in Java:

1. Get Certificate: easier way is point your browser to that url and when certificate is presented save it on your local folder or directory say in C:/certificates/test.cer
2. Now go to Security folder of your JRE installation directory. id you have JDK installed than it would be something like C:/Program Files/Java//jdk1.6.0_20/jre/lib/security
3 Execute following keytool command to insert certificate into keystore


keytool -import -keystore cacerts -file test.cer

Now this will print details about certificate and ask you for confirmation of adding certificates:

Trust this certificate? [no]:  y
Certificate was added to keystore
if you approve it by typing "y" certificate will be added into keystore.
Trust this certificate? [no]:  n
Certificate was not added to keystore


password : changeit  - by default.

if you decline it by typing "n" certificate will not be added into keystore.


Important point about SSL, KeyStore and keyTool in Java :- 
1. Certificates are required to access secure sites using SSL protocol or making secure connection from client to server.
2. JRE stores certificates inside keystore named as "cacerts" in folder C:/Program Files/Java/jdk1.6.0_20/jre/lib/security.
3. Common password of keystore is "Changeit"
4. Keytool is used to access keystore in Java and by using keytool you can list, add certificates from keystore.
5. if you are implementing SSL connection on Server side say Tomcat you need both keyStore and trustStore, both can be same file though. keyStore will be used to store server certificate which server will present to client on SSL connection.

That’s all on how to add and list certificates from keyStore or trustStore in java. Keytool utility which comes with JDK installation will help you to create alias, list certificates etc.

Sunday, 2 August 2015

How to print values of an object in Java when you do not have the source code for the class?

                 You can get all fields by Class#getDeclaredFields(). Each returns a Field object of which you in turn can use the get() method to obtain the value. To get the values for non-public fields, you only need to set Field#setAccessible() to true.

Example :-
-------------------------------------------------------------------------------------------------------------------
package com.test;

public class TestBean {
   
    String name;
    String job;
    String company;
    String sal;
   
    //Initialising the properties.
    TestBean() {
        this.name = "java";
        this.job = "developer";
        this.company = "XYZ";
        this.sal = "10000";
    }
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public String getSal() {
        return sal;
    }
    public void setSal(String sal) {
        this.sal = sal;
    }
}

------------------------------------------------------------------------------------------------------------------ 
package com.test;

import java.lang.reflect.Field;

public class PrintPropertiesFromObject {

    public static void main(String[] args) throws RuntimeException, IllegalAccessException {
       
        TestBean testBeanObject = new TestBean();
       
        for (Field field : testBeanObject.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            String name = field.getName();
            Object value = field.get(testBeanObject);
            System.out.printf("Field name: %s, Field value: %s%n", name, value);
        }
    }
}

 -----------------------------------------------------------------------------------------------------------------
Output :-
Field name: name, Field value: java
Field name: job, Field value: developer
Field name: company, Field value: XYZ
Field name: sal, Field value: 10000

Thursday, 23 July 2015

Optimization techniques in Sets

              Set is a collection of unique objects, it doesn't allow duplicate objects and modification of existing objects. Set types also allow basic operations like adding objects, removing objects, accessing objects, iterating objects but do not allow modification. There are two implementations of the Set interface they are HashSet and TreeSet.

            HashSet gives better performance than TreeSet because , TreeSet is an ordered collection of objects and the objects are sorted while they are inserted in the TreeSet where as in case of HashSet objects are added in an adhoc manner. It is expensive to do all basic operations in TreeSet because it has to compare and sort every object. We can get better performance by using a HashSet and converting  it to a TreeSet later on.

             HashSet and TreeSet are backed by HashMap and TreeMap respectively. Whenever we use a HashSet we can specify an initial capacity and load factor using constructors. The default size for a HashSet is 11 and it's load factor is 0.75. Load factor determines at which capacity HashSet has to be resized. It's internal structure will become double in size when it reaches it's maximum capacity based on load factor. HashSet scales well when it is initialized with proper size and default load factor. When you know the the number of objects to be added, it is better to initialize with that capacity and put load factor as 1.0f. The objects in HashSet are stored and retrieved through hash code which provides constant look up time.  I did not give any bench marks for these two Sets because We don't have many options here to compare and evaluate. We have two Sets to choose. Use TreeSet if you want sorted collection otherwise use HashSet.

The constructors for the HashSet to initialize with proper size are:

HashSet(int initialcapacity)

HashSet(int initialcapacity, float loadfactor)
Key Points :
  • Use HashSet for maintaining unique objects if you don't want thread safe for the collection for all basic(add/remove/access) operations otherwise use synchronized HashSet for thread safe.
  • Use TreeSet for ordered and sorted set of unique objects for non-thread safe collection otherwise use synchronized TreeSet for thread safe.

Tuesday, 21 July 2015

Optimization techniques in Lists

            List types represent an ordered collection of objects. ArrayList, Vector, Stack and LinkedList are the List implementation classes. All  List types support basic operations - adding objects, removing objects, accessing objects and iterating through the list. So which one to choose since all the list implementations support these basic operations? Performance is different for each class based on specific operations. So your choice is driven by the performance and the requirement options. Your requirement could be

1. Thread safe collection
2. Size of collection (large or small collection)
3. Type of operation ( adding, removing, accessing or iterating )


              If you want your collection to be thread safe then Vector or Stack must be used because both have synchronized methods. While ArrayList and LinkedList are not thread safe. Stack is meant for specific LIFO (last in - first out) operations, this can be filtered down based on this specific requirement. If you don't want your collection to be thread safe then you have can choose between ArrayList or LinkedList. General concept from performance point of view is that ArrayList gives better performance when accessing and iterating objects whereas LinkedList gives better performance when adding and removing objects. Although true in most cases, sometimes there is an exception.
conclusion is :
Type of operation ArrayList with out initialization ArrayList with initialization Vector with out initialization Vector with initialization LinkedList
Adding objects at end fast (but slower than initialization) fast fast (but sligtly slower than initialization and slower than ArrayList because of synchronization) fast(but sligtly slower than ArrayList because of synchronization) fast ( but slightly slower than ArrayList and Vector)
Adding objects at middle slow ( slower than when adding objects at last) slow ( slower than when adding objects at last) slow ( slower than when adding objects at last) slow ( slower than when adding objects at last) worse( worse than every operation)
Adding objects at first slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle)

                       The initial size for ArrayList and Vector is 10. ArrayList increases its capacity by half approximately whenever its capacity reaches maximum (10) but Vector increases its capacity by double whenever its capacity reaches maximum. That is the reason why ArrayList takes more time than Vector if it is not initialized with proper size though ArrayList is not synchronized. As soon as it reaches its maximum capacity when adding objects, it creates one more bigger array ( with 15 capacity for ArrayList approximately and 20 capacity for Vector) and copies the previous and new objects into new array. Obviously it is expensive to create new array and copy objects. So best approach is to initialize the ArrayList and Vector with proper size using constructors or using ensureCapacity(int capacity) which gives good performance.  If you initialize with proper size then the ArrayList gives better performance than Vector.
                        ArrayList with initialization gives better performance than others because its methods are non-synchronized. Synchronized methods are bit expensive because JVM has to lock the objects whenever it finds synchronized methods.
                        Vector takes slightly more time than ArrayList when you use JDK1.3 Hotspot JVM ,if you are not sure that whether your collection needs to be thread safe or not then it is better to use Vector to have higher safety.
                       You can convert an ArrayList as thread safe collection using Collections.synchronizedList(ArrayList object) but it is more expensive than using a Vector.
                      ArrayList and Vector maintain internal Object array ( Object[]) to store objects. So whenever you add an object, they add it to the end of the array which is  fine as long as it doesn't reach its maximum capacity. If you want to add an object at any other position, it creates a new object array and recopies all the objects which is expensive. That is the reason why adding objects at middle and beginning of collection takes a long time than when it is adding at the end
                     LinkedList gives good performance when adding elements at the end and beginning but it is worse when adding objects at middle because it needs to scan the node whenever it needs to add an object. LinkedList cannot be initialized.

The constructors for ArrayList and Vector to initialize with proper size are
ArrayList( int initialcapacity)
Vector( int initialcapacity)
Vector( int initialcapacity, int capacityIncrement)
You can give incremental capacity in Vector to change the default increment in capacity.
Here is the ListAddTest.java source code.
 ===================================================================
package com.test;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;

public class ListAddTest {

    private static final int NUM = 50000;
    private static String[] objs = null;

    public void addLast(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {            list.add(objs[i]);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for adding Objects at End: " + (endTime - startTime));
    }
   public void addFirst(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.add(0, objs[i]);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for adding Objects at First : " + (endTime - startTime));
    }
    public void addMiddle(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.add(i / 2, objs[i]);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for adding Objects at Middle : " + (endTime - startTime));
    }
    public void doTest(List list) {
        addLast(list);
        clear(list);
       
        addMiddle(list);
        clear(list);
       
        addFirst(list);
        clear(list);
    }
    public void clear(List col) {
        if (!col.isEmpty())
            col.clear();
    }
    public static void main(String[] args) {
        objs = new String[NUM];
        for (int i = 0; i < NUM; i++) {
            objs[i] = "Object" + i;
        }
        ListAddTest col = new ListAddTest();
        ArrayList collection1 = new ArrayList();
        col.doTest(collection1);

        ArrayList collection1A = new ArrayList(NUM);
        col.doTest(collection1A);

        Vector collection2 = new Vector();
        col.doTest(collection2);

        Vector collection2A = new Vector(NUM);
        col.doTest(collection2A);

        LinkedList collection4 = new LinkedList();
        col.doTest(collection4);
    }
}
 ====================================================================
Here is the Output :
Time taken for adding Objects at End: 0
Time taken for adding Objects at Middle : 101
Time taken for adding Objects at First : 180
Time taken for adding Objects at End: 0
Time taken for adding Objects at Middle : 90
Time taken for adding Objects at First : 190
Time taken for adding Objects at End: 0
Time taken for adding Objects at Middle : 100
Time taken for adding Objects at First : 180
Time taken for adding Objects at End: 10
Time taken for adding Objects at Middle : 90
Time taken for adding Objects at First : 180
Time taken for adding Objects at End: 10
Time taken for adding Objects at Middle : 1091
Time taken for adding Objects at First : 0

====================================================================== 
Removing objects :
  1. All classes take approximately same time when removing objects from end
  2. ArrayList and Vector give similar performance with slight difference because of JDK1.3 Hotspot JVM.
  3. LinkedList  gives worst performance when removing objects from middle (similar to adding objects at middle).
  4. LinkedList gives better performance when removing objects from the beginning.
  5. Only LinkedList gives better performance when removing objects from the beginning.
======================================================================
Here is the ListRemoveTest.java source code
package com.test;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import java.util.Arrays;

public class ListRemoveTest {

    private static final int NUM = 20000;
    private static Object[] objs = null;

    public void removeLast(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = NUM; i > 0; i--) {
            list.remove(i - 1);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for removing Objects at End: " + (endTime - startTime));
    }
    public void removeFirst(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.remove(0);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for removing Objects at First : " + (endTime - startTime));
    }
    public void removeMiddle(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.remove((NUM - i) / 2);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for removing Objects at Middle : " + (endTime - startTime));
    }
    public void doTest(List collection) {
        collection.addAll(getList());
        removeLast(collection);
        clear(collection);
 
        collection.addAll(getList());
        removeMiddle(collection);
        clear(collection);

        collection.addAll(getList());
        removeFirst(collection);
        clear(collection);
    }
    public void clear(List col) {
        if (!col.isEmpty())
            col.clear();
    }
    public List getList() {
        objs = new Object[NUM];
        for (int i = 0; i < NUM; i++) {
            objs[i] = new Object();
        }
        return Arrays.asList(objs);
    }
    public static void main(String[] args) {
        ListRemoveTest col = new ListRemoveTest();
      
        ArrayList collection1 = new ArrayList();
        col.doTest(collection1);

        Vector collection2 = new Vector();
        col.doTest(collection2);

        LinkedList collection4 = new LinkedList();
        col.doTest(collection4);
    }
}
=======================================================================
Here is the Output :
Time taken for removing Objects at End: 10
Time taken for removing Objects at Middle : 20
Time taken for removing Objects at First : 20
Time taken for removing Objects at End: 0
Time taken for removing Objects at Middle : 20
Time taken for removing Objects at First : 30
Time taken for removing Objects at End: 10
Time taken for removing Objects at Middle : 150
Time taken for removing Objects at First : 0
=======================================================================
The conclusion is :
  • ArrayList and Vector give best performance because they access objects using index. Vector takes slightly more time but it is negligible.
  • LinkedList gives worst performance  when accessing objects at end and middle because it has to scan nodes to access objects.
=========================================================================
Here is the ListAccessTest.java source code
package com.test;

import java.util.List;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import java.util.Arrays;

public class ListAccessTest {

    private static final int NUM = 25000;
    private static Object[] objs = null;

    public void getFromLast(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = NUM; i > 0; i--) {
            list.get(i - 1);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for getting Objects at Last: " + (endTime - startTime));
    }
    public void getFromFirst(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.get(0);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for getting Objects at First : " + (endTime - startTime));
    }
    public void getFromMiddle(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.get(NUM / 2);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for getting Objects at Middle : " + (endTime - startTime));
    }
    public void doTest(List list) {
        list.addAll(getList());
        getFromLast(list);
        getFromMiddle(list);
        getFromFirst(list);
    }
    public void clear(List col) {
        if (!col.isEmpty())
            col.clear();
    }
    public static List getList() {
        objs = new Object[NUM];
        for (int i = 0; i < NUM; i++) {
            objs[i] = new Object();
        }
        return Arrays.asList(objs);
    }
    public static void main(String[] args) {
        ListAccess col = new ListAccess();

        ArrayList collection1 = new ArrayList();
        col.doTest(collection1);

        Vector collection2 = new Vector();
        col.doTest(collection2);

        LinkedList collection4 = new LinkedList();
        col.doTest(collection4);
    }
}
 =======================================================================
Here is the Output :
Time taken for getting Objects at Last: 0
Time taken for getting Objects at Middle : 0
Time taken for getting Objects at First : 10
Time taken for getting Objects at Last: 0
Time taken for getting Objects at Middle : 0
Time taken for getting Objects at First : 0
Time taken for getting Objects at Last: 222
Time taken for getting Objects at Middle : 420
Time taken for getting Objects at First : 0
=======================================================================
Iterating collection:
                  Iterating collection using all three types of classes ,ArrayList ,Vector and LinkedList gives similar performance because they need not do any extra work  they simply iterate one by one. So I did not give any benchmark results. You can use any Iterator . But using ListIterator gives more flexibility than Iterator and Enumeration. You can traverse both sides.
 Key Points :
  • Use ArrayList with proper initialization if you don't want thread safe for the collection whenever you  add/remove/access objects at end and middle of collection.
  • Use Vector with proper initialization if you want thread safe for the collection whenever you  add/remove/access objects at end and middle of collection.
  • Use LinkedList if you don't want thread safe for the collection whenever you  add/remove/access objects at beginning of collection.
  • Use synchronized LinkedList if you want thread safe for the collection whenever you add/remove/access objects at beginning of collection.
  • Use ListIterator than Iterator and Enumeration for List types

Monday, 20 July 2015

Optimization techniques in Maps

          Map is a collection of key and value object associations. You can do all basic operations as in Lists and Sets such as adding , removing ,and accessing key-value pairs. There are four types of Map implementations they are HashMap, Hashtable, WeakHashMap and TreeMap.

         HashMap, Hashtable and WeakHashMap have similar implementations. TreeMap is meant for sorted collection, this can be filtered down based on the requirement. Then we have other three types to choose from. The choice  again depends upon your requirement. Your requirement could be

1.Thread safe
2.Type of operation ( basic operations )

HashMap and  WeakHashMap are not synchronized whereas Hashtable is synchronized.

          WeakHashMap is a special purpose map which uses an internal hashtable. When there are no more references to key object except weak reference maintained by WeakHashMap, the garbage collector reclaims the key object and mapping between key object and value object is also reclaimed, if the value object does not have any other references then the value object is also reclaimed by the garbage collector.

          If you want your Map type collection to be thread safe, then you need to use Hashtable otherwise use HashMap. HashMap gives better performance than Hashtable because of it's non-synchronized methods. The reason I did not give any bench marks for Map types is that it is pretty straight forward to choose Map type depending on requirement .

        You can improve performance by using proper initial size and load factor in the constructor for all types of Map types except TreeMap.

The constructors are

HashMap(int initialcapacity)
HashMap(int initialcapacity, float loadfactor)
Hashtable(int initialcapacity)
Hashtable(int initialcapacity, float loadfactor)
WeakHashMap(int initialcapacity)
WeakHashMap(int initialcapacity, float loadfactor)

            When the number of objects exceed loadfactor capacity, then the capacity of the class increases to (2*capacity + 1). The default load factor is 0.75.  

            All these classes work in accordance with hash values which are used to identify the value objects. The key objects are converted to integer called hash code by using hashing algorithms which is used as an index for value objects. Hash code must be same for two equal objects, i.e. when tested with the equals() method,it must return true.  The hash code is determined by using hashCode() method. The hash code of a collection is determined by cumulating all the hash codes of the associated objects. 
Key Points :
  • Use HashMap for non-thread safe map collection otherwise use Hashtable for thread safe collection. 
  • Use TreeMap for non-thread safe ordered map collection otherwise use synchronized TreeMap for thread safe.

Sunday, 19 July 2015

Performance improvement techniques in Object creation

 This topic illustrates the performance improvement techniques in object creation with the following sections:
  •     Overview of Objects
  •     Optimization techniques in Object creation
  •     Key Points
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
  •     Avoid creating objects in a loop.
  •     Use String literals instead of String objects (created using the 'new' keyword) if the content    is same.
  •     Make used objects eligible for garbage collection.
  •     Do not keep inheritance chains long.
  •     Accessing local variables is faster than accessing class variables
  •     Use lazy evaluation, lazy object creation whenever possible.

Thursday, 16 July 2015

Performance improvement techniques in Serialization

This topic illustrates the performance improvement techniques in Serialization with the following sections:
  •     Overview of Serialization
  •     Optimization with 'transient'
  •     Key Points
Overview of Serialization
                      Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket.

Deserialization is the process of reading back that serialized java object stream from input stream.

A java object is serializeable and deserializeable if that class follows the following rules

A) The java class must implement java.io.Serializable interface or java.io.Externalizable interface or inherit that implementation from any one of it's super class implementation.

B) All instance variables of that class must implement Serializable interface or Externalizable interface or inherit from one of it's super class.

All primitive data types and some of standard java API classes are serializable. You need not explicitly implement Serializable or Externalizable interfaces for those classes. Serialization process ignores class (static) variables.

Externalizable interface allow to do your own custom implementation of serialization. In this section,  focus is only on Serializable interface.

We will talk initially about Serializable interface. This is a marker interface and does not have any methods. All major java technologies like RMI, EJB are based on serialization process to pass the objects through network. These technologies implicitly do all the serialization work for you. You need to simply implement the java.io.Serialzable interface, but If you want to do your own serialization, that is reading from or writing to streams, ObjectInputStream and ObjectOutputStream  can be used.

These methods help to write into stream and read from stream

ObjectInputStream.readObject();                    // to read object

ObjectInputStream.writeObject(Object obj);  // to write object

Initially, We need to understand the default mechanism of serialization process in order to improve performance

The default mechanism:
                     When you write or read an object to a file or network or other stream using serialization process, It writes/reads the complete object state that means it writes the object, it's instance variables, and  super class instance variables except transient variables and class (static) variables. Look at this object hierarchy.



                               In this class hierarchy, when I write CorporateEmployee object into file and and read from that file, Initially Address is called, second HomeAddress is called, third Employee is called and finally CorporateEmployee is called. So Total object hierarchy will be written into file except transient and class (static) variables. Initially super class will be called and so on till end of heirarchy. You need to keep an eye on this mechanism and act up on that, otherwise you will end up with writing everything. The next section explains how to avoid unnecessary data in to streams and improve performance.

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

                        Variables that have access modifier  'transient'  will not be read from or written into streams. It gives facility to avoid writing unnecessary data into streams. In other words, it boosts the performance by avoiding writing unnecessary data into streams.
Here is the code snippet to show the Serialization process with transient and non transient variation bench marks
 ------------------------------------------------------------------------------------------------------------
package test;

import java.util.Vector;
import java.io.*;

public class SerializationTest {

    static long start, end;
    OutputStream out = null;
    InputStream in = null;
    OutputStream outBuffer = null;
    InputStream inBuffer = null;
    ObjectOutputStream objectOut = null;
    ObjectInputStream objectIn = null;

    public Person getObject() {

        Person p = new Person("SID", "austin");
        Vector v = new Vector();

        for (int i = 0; i < 7000; i++) {
            v.addElement("StringObject" + i);
        }
        p.setData(v);
        return p;
    }

    public static void main(String[] args) {

        SerializationTest st = new SerializationTest();
        start = System.currentTimeMillis();
        st.writeObject();
        st.readObject();
        end = System.currentTimeMillis();
        System.out.println("Time taken for writing and reading :"
                + (end - start) + "milli seconds");
    }
    public void readObject() {
        try {
            in = new FileInputStream("c:/temp/test.txt");
            inBuffer = new BufferedInputStream(in);
            objectIn = new ObjectInputStream(inBuffer);
            objectIn.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (objectIn != null)
                try {
                    objectIn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    public void writeObject() {
        try {
            out = new FileOutputStream("c:/temp/test.txt");
            outBuffer = new BufferedOutputStream(out);
            objectOut = new ObjectOutputStream(outBuffer);
            objectOut.writeObject(getObject());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (objectOut != null)
                try {
                    objectOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

class Person implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private String name;
    private Vector data;
    private String address;

    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public Vector getData() {
        return data;
    }

    public String getName() {
        return name;
    }

    public void setData(Vector data) {
        this.data = data;
    }
}
-------------------------------------------------------------------------------------------------------------
It writes the Person Object into file and reads from that file.

The output is :
Time taken for writing and reading : 390 milli seconds
If I use 'transient' modifier for the Vector in the Person Object, then the output is
Time taken for writing and reading : 110 milli seconds

It almost increases the speed more than 3 times.
You need to use 'transient' keyword for unnecessary variables to increase performance.

Key Points
  • Use 'transient' key word for unnecessary variables that need not be read from/written into streams.
  • When you write RMI, EJB or any other technologies that uses built in Serialization to pass objects through network, use 'transient' key word for unnescessary variables.
  • Class (static) variables ignores by Serialization process like 'transient' variables.

Performance improvement techniques in Exceptions

This topic illustrates the performance improvement techniques in Exceptions with the following sections:
  •     Overview of Exceptions
  •     Optimization techniques in Exceptions
  •     Key Points
Overview of Exceptions
                 Java provides an efficient way to handle unexpected conditions that can occur in the program. When there is an error or bug in the program then the program terminates as soon as an error is encountered leaving the program in an in consistent state, to avoid this Java makes use of Exception Handling mechanism to a great advantage so that when ever there is an exceptional condition then the program handles it gracefully and continues program execution.

The whole concept of exceptions/errors is handled by the java.lang.Throwable class. It has two subclasses - Error and Exception. We generally need not handle Errors, they are handled by JVM. Example of Error is OutOfMemoryError.

Exceptions are of two types -Checked exceptions and Unchecked exceptions.

Checked exceptions should either be declared in the throws clause or caught  in the catch block. Unchecked exceptions need not be declared in the throws clause but can to be caught in the catch clause.

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

Optimization techniques in Exceptions
                In a catch block avoid using the generic class Exception. For each try block use specific catch blocks based on what can go wrong in your code.

    Do not use Exception handling for anything other than exception handling like to control the flow of your program.

    Whenever you are using a throws clause always use the specific subclass of Exception like FileNotFoundException rather than using throws Exception.

    Use exception handling generously-Very little overhead is imposed by using exception handling mechanism unless an exception occurs. But when an exception occurs it imposes an overhead in terms of execution time.

    Always use the finally block to release the resources like a database connection, closing a file or socket connection etc. This  prevents resource leaks even if an exception occurs.

    When using method calls always handle the exceptions in the method where they occur, do not allow them to propagate to the calling method unless it is specifically required. It is efficient to handle them locally since allowing them to propagate to the calling method takes more execution time.

    Do not use Exception handling in loops. It is better to place loops inside try/catch blocks than vice versa. Here is an code snippet that gives bench mark.

 package com.performance.exception;

public class ExceptionTest{

public static void main(String args[]){

             long start,end;
             int i =0;

             int[] intArray = new int[25000];
             String stringArray[] = new String[25000];
             int size = stringArray.length;

             for(i=0;i<size;i++){
                     if(i%50 == 0)
                            stringArray[i]="hello world";
                       else
                             stringArray[i]= Integer.toString(i);
             }

             start=System.currentTimeMillis();
             for(i=0;i<size;i++){
                 try{
                         intArray[i]=Integer.parseInt(stringArray[i]);
                 } catch(NumberFormatException e) {
                    }
              }
             end=System.currentTimeMillis();
             System.out.println(end-start + " millis with try/catch inside for loop ");
             start=System.currentTimeMillis();

             try{
                for(i=0;i<size;i++){
                      intArray[i]=Integer.parseInt(stringArray[i]);
                }
             } catch(NumberFormatException e) {
               }
             end=System.currentTimeMillis();
             System.out.println(end-start + " millis with try/catch outside for loop ");
    }
}

The output is
     50 millis with try/catch inside for loop
     0 millis with try/catch outside for loop

Key Points
  • Be specific while handling the exception in your catch block.
  • Be specific while throwing exception in your throws clause.
  • Do not use Exception Handling to control programming flow.
  • Very little overhead is imposed by using exception handling mechanism unless an exception occurs or thrown a new exception object explicitly.
  • Always use the finally block to release the resources to prevent resource leaks.
  • Handle exceptions locally wherever possible.
  • Do not use Exception handling in loops.

Wednesday, 15 July 2015

How to fix GC overhead limit exceeded in Eclipse?

Eclipse will throw GC overhead limit exceeded error when it runs out of memory, normally while performing memory-consuming operations such as building workspace on big projects, this will also increase the performance of building and deploying your projects.

The error would look something like this:

An internal error occurred during: "Building workspace". GC overhead limit exceeded

To fix this problem, you'll need to allocate more memory to your Eclipse instance. To do this, locate the eclipse.ini file in your Eclipse's installation directory. The content would be something similar to the following;

-startup 
plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar 
--launcher.library 
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.200.v20140116-2212 
-product 
org.eclipse.epp.package.jee.product 
--launcher.defaultAction openFile 
-showsplash 
org.eclipse.platform 
--launcher.XXMaxPermSize 
256m
--launcher.defaultAction 
openFile 
--launcher.appendVmargs 
-vmargs 
-Dosgi.requiredJavaVersion=1.6 
-XX:MaxPermSize=256m 
-Xms40m 
-Xmx512m
 
To increase the memory allocation for your Eclipse instance, edit the number in the following lines accordingly.
-Xms40m  to -Xms512m 
-Xmx512m to -Xmx1024m based on your system Ram.

 These options are passed to Java when launching Eclipse. Xms specifies the initial memory allocation pool, and Xmx specifies maximum memory allocation pool for Java Virtual Machine (JVM). The number is the amount of memory, in Megabytes.

You can also increase the value of MaxPermSize, as the following;
-XX:MaxPermSize=1024m

MaxPermSize or Permanent Generation defines the memory allocated to keep compiled class files.
Restart Eclipse for the changes to take effect.

Sunday, 12 July 2015

Optimization techniques when Concatenating Strings


You can concatenate multiple strings using either + operator or String.concat()  or   StringBuffer.append(). Which is the best one interms of performance?
The choice depends on two scenarios,first scenario is compile time resolution versus run time resolution and second scenario is wether you are using StringBuffer or String. In general, programmers think that StringBuffer.append() is better than + operator or String.concat() method. But this assumption is not true under certain conditions.

1) First scenario: compile time resolution versus run time resolution
Look at the following code StringAppendTest .java and the output.

package com.citruspay;

public class StringAppendTest {

    /**
     * This class shows the time taken by string concatenation at compile time
     * and run time.
     */

    public static void main(String[] args) {

        // Test the String Concatination
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 5000; i++) {
            String result = "This is" + "testing the" + "difference"
                    + "between" + "String" + "and" + "StringBuffer";
        }

        long endTime = System.currentTimeMillis();

        System.out.println("Time taken for string concatenation using + operator : "
                        + (endTime - startTime) + " milli seconds");

        // Test the StringBuffer Concatination
        long startTime1 = System.currentTimeMillis();

        for (int i = 0; i < 5000; i++) {

            StringBuffer result = new StringBuffer();

            result.append("This is");
            result.append("testing the");
            result.append("difference");
            result.append("between");
            result.append("String");
            result.append("and");
            result.append("StringBuffer");
        }

        long endTime1 = System.currentTimeMillis();

        System.out.println("Time taken for String concatenation using StringBuffer : "
                        + (endTime1 - startTime1) + " milli seconds");
    }
}

The output of this code
Time taken for String concatenation using + operator : 0 milli seconds
Time taken for String concatenation using StringBuffer : 50 milli seconds

Interestingly the + operator is faster than StringBuffer.append() method. Let us see why?

Here the compiler does a good job of optimization. Compiler simply concatenates at compile time as shown below. It does compile time resolution instead of runtime resolution, this happens when you create a String object using 'new' key word.
before compilation:
String result = "This is"+"testing the"+"difference"+"between"+"String"+"and"+"StringBuffer";
after compilation
String result = "This is testing the difference between String and StringBuffer";
String object is resolved at compile time where as StringBuffer object is resolved at run time. Run time resolution takes place when the value of the string is not known in advance where as compile time resolution happens when the value of the string is known in advance. Here is an example.
Before compilation:
public String getString(String str1,String str2) {
            return str1+str2;
}

After compilation:
           return new StringBuffer().append(str1).append(str2).toString();
This resolves at run time and take much more time to execute.

2) Second scenario: Using StringBuffer instead of String
If you look at the following code, you will find StringBuffer is faster than String for concatenation which is opposite to above scenario.

package com.demo;

public class StringBufferAppendTest {

    public static void main(String[] args) {

        // Test the String Concatenation using + operator
        long startTime = System.currentTimeMillis();

        String result = "hello";

        for (int i = 0; i < 1500; i++) {
            result += "hello";
        }

        long endTime = System.currentTimeMillis();

        System.out.println("Time taken for string concatenation using + operator : "
                    + (endTime - startTime) + " milli seconds");

        // Test the String Concatenation using StringBuffer
        long startTime1 = System.currentTimeMillis();

        StringBuffer result1 = new StringBuffer("hello");

        for (int i = 0; i < 1500; i++) {
            result1.append("hello");
        }

        long endTime1 = System.currentTimeMillis();

        System.out.println("Time taken for string concatenation using StringBuffer :  "
                    + (endTime1 - startTime1) + " milli seconds");
    }
}

The output of the code is:
Time taken for string concatenation using + operator : 280 milli seconds
Time taken for String concatenation using StringBuffer : 0 milli seconds

It shows StringBuffer.append() is much more faster than String. Why?
The reason is both resolve at runtime but the + operator resolves in a different manner and uses String and StringBuffer to do this operation.

Optimization by initializing StringBuffer
You can set the initial capacity of StringBuffer using its constructor this improves performance significantly. The constructor is StringBuffer(int length), length shows the number of characters the StringBuffer can hold.

You can even set the capacity using ensureCapacity(int minimumcapacity) after creation of StringBuffer object. Initially we will look at the default behavior and then the better approach later.

The default behavior of StringBuffer:
StringBuffer maintains a character array internally.When you create StringBuffer with default constructor StringBuffer() without setting initial length, then the StringBuffer is initialized with 16 characters. The default capacity is 16 characters. When the StringBuffer reaches its maximum capacity, it will increase its size by twice the size plus 2 ( 2*old size +2).

 If you use default size, initially and go on adding characters, then it increases its size by 34(2*16 +2) after it adds 16th character and it increases its size by 70(2*34+2) after it adds 34th character. Whenever it reaches its maximum capacity it has to create a new character array and recopy old and new characters. It is obviously expensive. So it is always good to initialize with proper size that gives very good performance.

I tested the above StringTest4.java again with two StringBuffers, one without initial size and other with initial size. I added 50000 'hello' objects this time and did not use the + operator. I initialized the second StringBuffer with StringBuffer(250000).

The output is :
Time taken for String concatenation using StringBuffer with out setting size: 280 milli seconds
Time taken for String concatenation using StringBuffer with setting size: 0 milli seconds

It shows how effective the initialization of StringBuffer is. So it is always best to initialize the StringBuffer with proper size.

Key Points

  • Create strings as literals instead of creating String objects using 'new' key word whenever possible.
  • Use String.intern() method if you want to add number of equal objects whenever you create String objects using 'new' key word.
  • + operator gives best performance for String concatenation if Strings resolve at compile time.
  • StringBuffer with proper initial size gives best performance for String concatenation if Strings resolve at run time.
We appreciate and welcome your comments on this section.

Performance improvement techniques in String and StringBuffer


This topic illustrates the performance improvement techniques in String and StringBuffer with the following sections:
  • Overview of String and StringBuffer
  • Better way of creating Strings
  • Optimization by interning Strings
  • Optimization techniques when Concatenating Strings
  • Optimization by initializing StringBuffer
  • Key Points
Overview of String and StringBuffer :
Immutable objects cannot be modified once they are created. Mutable objects can be modified after their creation. String objects are immutable where as StringBuffer objects are mutable.
You need to carefully choose between these two objects depending on the situation for better performance. The following topics illustrate in detail :
Note: This section assumes that reader has some basic knowledge of Java Strings and StringBuffer.

Better way of creating Strings :
You can create String objects in the following ways.
1. String s1 = "hello";
   String s2 = "hello";

2. String s3 = new String("hello");
   String s4 = new String("hello");

Which of the above gives better performance?
Here is a code snippet to measure the difference.

package com.demo;

public class StringTest {

    public static void main(String[] args) {

        // create String literals
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 50000; i++) {
            String s1 = "hello";
            String s2 = "hello";
        }

        long endTime = System.currentTimeMillis();

        System.out.println("Time taken for creation of String literals : "
                    + (endTime - startTime) + " milli seconds");
       
        // create String objects using 'new' keyword
        long startTime1 = System.currentTimeMillis();

        for (int i = 0; i < 50000; i++) {
            String s3 = new String("hello");
            String s4 = new String("hello");
        }

        long endTime1 = System.currentTimeMillis();

        System.out.println("Time taken for creation of String objects : "
                    + (endTime1 - startTime1) + " milli seconds");
    }
}

The output of this code
Time taken for creation of String literals : 0 milli seconds
Time taken for creation of String objects : 170 milli seconds

It clearly shows first type of creation is much more faster than second type of creation. Why?
Because the content is same s1 and s2 refer to the same object where as s3 and s4 do not refer to the same object. The 'new' key word creates new objects for s3 and s4 which is expensive.
 
How the JVM works with Strings:
                 Java Virtual Machine maintains an internal list of references for interned Strings ( pool of unique Strings) to avoid duplicate String objects in heap memory. Whenever the JVM loads String literal from class file and executes, it checks whether that String exists in the internal list or not. If it already exists in the list, then it  does not create a new String and it uses reference to the existing String Object. JVM does this type of checking internally for String literal but not for String object which it creates through 'new' keyword. You can explicitly force JVM to do this type of checking for String objects which are created through 'new' keyword using String.intern() method. This forces JVM to check the internal list and use the existing String object if it is already present.
So the conclusion is, JVM maintains unique String objects for String literals internally. Programmers need not bother about String literals but they should bother about String objects that are created using 'new' keyword and they should use intern() method to avoid duplicate String objects in heap memory which in turn improves java performance. see the following section for more information.
The following figure shows the creation of String Objects without using the intern() method.


You can test the above difference programmatically using == operator and String.equals() method.
== operator returns true if the references point to the same object but it does not check the contents of the String object where as String.equals() method returns true if the contents of the String objects are equal.
s1==s2 for the above code returns true because s1 and s2 references point to the same object.
s3.equals(s4) for the above code returns true because both objects content  is same which is "hello".
You can see this mechanism in the above figure. Here, we have three separate objects which contain same content,"hello". Actually we don't need separate objects because they use memory and take time to execute.
How do you make sure that the String objects are not duplicated?
The next topic covers this interesting interning String mechanism.

Optimization by Interning Stings
In situations where String objects are duplicated unnecessarily, String.intern() method avoids duplicating String objects. The following figure shows how the String.intern() method works. The String.intern() method checks the object existence and if the object exists already, it changes point of reference to the original object rather than create a new object.
The following figure shows the creation of String literal and String Object using intern

Here is the sample code to know the importance of String.intern() method..

package com.demo;

public class StringInternTest {

    public static void main(String[] args) {

        // create String references like s1,s2,s3...so on..
        String variables[] = new String[50000];

        for (int i = 0; i < variables.length; i++) {

            variables[i] = "s" + i;
        }

        // create String literals
        long startTime0 = System.currentTimeMillis();

        for (int i = 0; i < variables.length; i++) {

            variables[i] = "hello";
        }

        long endTime0 = System.currentTimeMillis();

        System.out.println("Time taken for creation of String literals : "
                    + (endTime0 - startTime0) + " milli seconds");

        // create String objects using 'new' keyword
        long startTime1 = System.currentTimeMillis();

        for (int i = 0; i < variables.length; i++) {

            variables[i] = new String("hello");
        }

        long endTime1 = System.currentTimeMillis();

        System.out.println("Time taken for creation of String objects with 'new' key word : "
                    + (endTime1 - startTime1) + " milli seconds");

        // intern String objects with intern() method
        long startTime2 = System.currentTimeMillis();

        for (int i = 0; i < variables.length; i++) {
            variables[i] = new String("hello");
            variables[i] = variables[i].intern();
        }

        long endTime2 = System.currentTimeMillis();

        System.out.println("Time taken for creation of String objects with intern(): "
                + (endTime2 - startTime2) + " milli seconds");
    }
}

Here is the output of the above code
Time taken for creation of String literals : 0 milli seconds
Time taken for creation of String objects with 'new' key word : 160 milli seconds
Time taken for creation of String objects with intern(): 60 milli seconds