Sunday, 5 August 2012


Asynchronous Servlet Example                                                           Spring Autowiring


Almost Everybody always have a curiosity that how JVM maintains objects into heap and when garbage collector works  to take out objects from heap ?How object allocate and release from the memory?what is the algorithm JVM follow? so here are some of the answers.

As we know the JVM's heap stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches

Basically, the Java virtual machine is organized into three generations: a young generation or new generation, an old generation or tenured generation, and a permanent generation. Most objects are initially allocated in the young generation. The old generation contains objects that have survived some number of young generation collections, as well as some large objects that may be allocated directly in the old generation. The permanent generation is somewhat special and it is used to store Meta data related to classes and method in JVM, it also hosts String pool provided by JVM.

New Feature implemented in JVM implementation in J2SE 5

There are lots of more feature implemented into the implementation of java 5 but now I am talking about Ergonomics feature which provides good performance from the JVM with a minimum of command line tuning . From the java 5 JVM is basically known as smart JVM because before java 5 the initial heap size of 4Mbyte and max heap size of 64Mbyte but from java 5 initial heap size of 1/64 of physical memory up to 1 Gbyte and max heap size of 1/4 of physical memory up to 1 Gbyte.
 The goal of ergonomics is to provide good performance from the JVM with a minimum of command line tuning. Ergonomics attempts to match the best selection of Garbage collector and Heap size  for an application. This selection assumes that the class of the machine on which the application is run is a hint as to the characteristics of the application (i.e., large applications run on large machines). In addition to these selections is a simplified way of tuning garbage collection. With the throughput collector the user can specify goals for a maximum pause time and a desired throughput for an application. This is in contrast to specifying the size of the heap that is needed for good performance. This is intended to particularly improve the performance of large applications that use large heaps. 

JVM Parameters for garbage collection in Java

Garbage collection tuning is a long exercise and requires lot of profiling of application and patience to get it right.  I have worked with some of the project where we need to increase the performance of Java application by profiling and finding what causing full GC and I found that Garbage collection tuning largely depends on application profile, what kind of object application has and what are there average lifetime etc. for example if an application has too many short lived object then making Eden space wide enough or larger will reduces number of minor collections. you can also control size of both young and Tenured generation using JVM parameters for example setting -XX:NewRatio=3 means that the ratio among the young and old generation is 1:3 , you got to be careful on sizing these generation. As making young generation larger will reduce size of tenured generation which will force Major collection to occur more frequently which pauses application thread during that duration results in degraded or reduced throughput. The parameters NewSize and MaxNewSize are used to specify the young generation size from below and above. Setting these equal to one another fixes the young generation.

Some Key points to improve the performance of application are:

1) Always nullify to objects at the end of their work.
2) Uses of try and catch block should be less.
3) Use interface instead of Utility class for constants even trying to use enums as much as possible.
4)Use String Builder instead of String Buffer class.
5)Use lazy initialization when there are objects or variables that may never be used, or when you need to distribute the load of creating objects
6)For small collections ArrayList and LinkedList are close in performance, though ArrayList is generally the faster of the two.


1) Java Heap is divided into three generation for sake of garbage collection. These are young generation, tenured or old generation and Perm area.
2) New objects are created into young generation and subsequently moved to old generation.
3) String pool is created in Perm area of Heap, garbage collection can occur in perm space but depends upon JVM to JVM.
4) Whenever Major garbage collection occurs application threads stops during that period which will reduce application’s performance and throughput.
5) JVM command line options –Xmx and -Xms is used to setup starting and max size for Java Heap. Ideal ratio of this parameter is either 1:1 or 1:1.5 based upon my experience for example you can have either both –Xmx and –Xms as 1GB or –Xms 1.2 GB and 1.8 GB.

For asynchronous feature of Servlet click on below link:-

No comments:

Post a Comment