Garbage collection is quite important if you are working in finance domain.No mater which Finance domain you work you always need to tune garbage collection parameter to get maximum performance.

So let us answer the question What is GC first.

Not like C++, Java does not have a de-constructor to explicitly specify a memory and remove it. Some people argues we can use System.gc(); System.gc() method will affect the system performance drastically! Set your object to null is a good idea but you still do not know when your object will be removed. In Java, the GC can find the unnecessary (garbage) objects and removes them automatically and unpredictably.

Strong reference object may more likely to cause memory leak. Thus instead of using HashMap map=new HashMap(); you can use WeakHashMap map=new WeakHashMap();

http://bit.ly/GTBGXl tells us “A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.”

Generally an object becomes eligible for garbage collection in Java on following cases:
1) All references of that object explicitly set to null e.g. object = null
2) Object is created inside a block and reference goes out scope once control exit that block.
3) Parent object set to null, if an object holds reference of another object and when you set container object’s reference null, child or contained object automatically becomes eligible for garbage collection.
4) If an object has only live references via WeakHashMap it will be eligible for garbage collection.

Tuning of GC

GC tuning is really a tough job. The easiest way to do that is to control the object created in your application. If millions of projects are created in your method, you will expect out of memory exceptions. Thus String is not recommended, use StringBuffer/StringBuilder instead. I recommend sometimes you can use String.intern(); as well to decrease the objects created in the String pool.

The second key should be the -Xms, -Xmx and  -XX:NewRatio options.  Ideal ratio of this parameter is either 1:1 or 1:1.5 based on my experience for example you can have either both –Xmx and –Xms as 1GB or –Xms 1 GB and 1.5 GB.

The third key is to use the suggested GC policy settings(http://bit.ly/17dwBVM) By tweaking those settings the performance of my latest project improved tremendously.

Below is a sample

-XX:+UseParNewGC

The option uses an improved collection algorithm that takes advantage of multiple processor cores to sweep the young generation. Since this is a stop-the-world phase of garbage collection, performance is improved if the collector uses multiple CPU cores while collecting the young generation.

The tutorial below provides more details about how to tune GC.

http://bit.ly/RdmLNI

How do I know there is a GC issue?

You can easily check GC status by using the jstat command; however, the best way to analyze GC is by generating logs with the –verbosegc option.

If you use Eclipse you can use a plug in called Eclipse Memory Analyser (MAT) . It will identify memory leaks.

Another tool is called nmon analyser, which is a free tool to produce AIX/Linux performance reports. http://ibm.co/M4biOB is an instruction of this free tool. It is used in my latest project.

Advertisements