Saturday, September 17, 2011

Java: Object Life Cycle

A java program creates many objects from different classes. Object in Java, interact by sending messages to each other.
After they are done doing processing, these objects are then garbage collected. After garbage collection process, the Operating System claims back the resources allocated to these objects which will be further used by new objects.

Below are the phases on which a Java object goes through out its life cycle:

1. Class Loading

Before creating object from a class, the class should be loaded to memory from the disk. The java class loader loads the class file to memory.

   When class is loaded?

  • when first time the object is being created.

  • any static field or method is accessed first time.


2. Static initializers

Java looks for any static initializers and initializes the static fields which are part of the class and not part of a specific instance of the class (object).

3. Object creation

Object is an instance of the class. It is created in below situations:

  • Declaration: when you declare object e.g. ClassA objA;

  • Instantiation: when new is used to allocate new object in heap memory e.g. new ClassA();

  • Initialization: new object is constructed e.g. ClassA();


4. Usage of the object

at this stage, programs could use the object either by accessing the fields or calling methods.

5. Cleanup

This is the last phase of the Java objects where they get recycled and the memory is claimed by the OS.
what happens on destruction?

  • Object is removed from the memory.

  • Java drops its internal references to this object.

  • Garbage Collection (GC) , runs which frees objects which are not needed any more i.e. there are no references to this object.

  • Finalization: GC gives objects a last chance opportunity to cleanup any other resources by calling its finalize() method.


 when it happens?


  • when object goes out of the scope. i.e. {...objA...}, here } becomes scope. at this time, Java run time checks for the references and lets GC recycle this object.

  • when number of references to this object in java run time memory becomes zero (0).

  • when object is explicitly set to null i.e. objA = null;, GC is called

  • finalize() method is explicitly called.

Wednesday, September 7, 2011

Difference between Java Vector and ArrayList

The Vector is an analog to the ArrayList. So, from an API perspective, the two classes are very similar.  They both use java Array internally to store the elements. There are still some major differences between these two classes and we will be going through them now...

Synchronization:

Vectors are synchronized. This means that all of its methods are thread safe.

ArrayList on the other hand, is unsynchronized, making them, therefore, not thread safe.

Using synchronization will incur a performance hit. What it means is that all the threads accessing the same Vector object are blocked until the first thread completes its operation on the object. Due to this no two threads are using the object at the same time. Hence this enforces the data consistency.

Tips: use the Vector only where it is really necessary.

Capacity and Data growth:

As we discussed above, Vector and ArrayList both hold their data in Java Array object. However when size of the Array goes beyond its allocated space, there needs to be resizing to accommodate the new elements in the Array. The expansion of the Array object is done differently for Vector and ArrayList:

Vector defaults to doubling the size of its array.

ArrayList increases its array size by 50 percent.

Tips: There is a cost involved in adding new elements to these containers. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later.
It is advisable to initialize the capacity of these containers at the time of object creation.

e.g. Vector v = new Vector(initialCapacity);

Depending on the application, choose right value for initialCapacity.

Usage patterns


These containers perform Add/Remove/Get in constant time i.e. O(1) when Add/Remove is done either at the start or end of the list. However the cost increases when Add/Remove is done inside the list. Because in this case, the iterator has to identify the position in linear time i.e. O(n).

Tips: use plain old Array in place of these containers for the performance critical applications.

Speed

ArrayList is faster than Vector. Since Vector is synchronized it pays price of synchronization which makes it little slow.

Default Size

ArrayList in Java has no default size but Vector in Java has default size of 10.

 

that's all folks!!!