Garbage collector is one of the most toughest code that is written under JVM. Highly optimized but not yet fully optimized code !
Most of the time we think as an application developer on Java, that GC is something which is not under our control and handled by JVM only and this is one of the place where we are really killing our application. Java HotSpot (JVM) gives 'n' number options to optimize GC according to the application need and to be frank this 'n' is 63. Some of the optimization is now deprecated in Mustang(jdk1.6) .
We can have several questions like how we can optimize our code!
Let me tell one perfect analogy that I saw someday back on JavaWorld for GC. Here it goes :
You are owner of a city, small city say 'JavaVelle' :) and in your city all the ppl are software eng'er. They are using too much of Computer and Car . Being small city there is only one main road in your city. Now, Dumping of computers and Cars is becoming too much and now you as an owner of the city want some garbage ppl to come and collector the garbage.
So, he is all the time coming with a big truck and collecting all the garbage and then putting it out of the city. But his truck is so big that when his truck is on road other ppl cant move here and there, and hence creating a congestion and not only that the biggest problem is Police and Ambulance type of critical process cant stop because of Garbage Truck.
This is the true scenario with GC. When JVM is running the GC to collect the Kachara (Garbage) it stop all the current running process. But some process which are real time or some which has too many client to serve cant stop like that. Sun provides a smart solution in Jdk 1.4 and smartest in Tiger release(JDK 1.5). It really based on some hopeless assumptions, like New object will die early, but it holds good. We are not going to discuss the whole process but as an application developer on Java, We can optimize the GC according to our own need. How ???
If you have a single-processor client machine and are having problems with pause times in your application, try the incremental garbage collector. This might help with perceived performance, even though it may decrease real application performance somewhat. Remember that perceived performance is usually more important than real performance for client applications. If the incremental garbage collector doesn't help (or doesn't help enough), and you have plenty of RAM, try the concurrent garbage collector.
If you have a single-processor server machine with lots of memory and experience trouble with application pause times, try the concurrent garbage collector.
If you have a multiprocessor machine, especially with four or more processors, try one of the parallel garbage collection algorithms. These should significantly decrease pause times. If you have lots of memory (gigabytes), use the scavenging collector; otherwise, use the copying collector.
-XX:+UseParNewGC Parallel copying
-XX:+UseParallelGC Parallel scavenging
-Xincgc Incremental [none] Mark-compact [default]
If you just want to check a demo, make a program with Lot of GC work. And then try these commands.
java -XX:+UseParNewGC Test
java -XX:+UseParallelGC Test
java -Xincgc Test
For web application developer, we need to make change in Web Container. I havn't try this but will do soon :)
Really want to go in detail, please check the Title Link.