G1 Garbage Collector Considered Slow?
I was recently considering trying out the new G1 garbage collector, see if it was any better than current real time CMS garbage collector. A concurrent soft real-time garbage collector that can compact? Awesome!
I switched one of my production applications to use the new G1 garbage collector and noticed a spike in CPU and diminishing throughput almost instantaniously, what gives? I googled around and stumbled upon this blog post and decided to do my own benchmarking.
I hacked up the following scala script based off the blog post to compare the two garbage collectors. The JDK that was used was JDK7u3 on Solaris on a quad core box.
val map = new java.util.LinkedHashMap[Long, String]() var i:Long = 0 while(i < math.pow(10000, 2)) { map.put(i, new String()) map.remove(i - 1) i = i + 1 if (i % 10000 == 0) { System.out.println(i) } }
CMS:
time scala -J-XX:+UseConcMarkSweepGC GC.scala
real 0m12.477s
user 0m12.364s
sys 0m0.491s
G1:
time scala -J-XX:+UseG1GC GC.scala
real 2m26.121s
user 7m33.234s
sys 0m10.888s
Conclusion:
Just what I saw with my production application, the throughput substantially diminished and the CPU cores spiked. I won't be using the G1 garbage collector any time soon, hopefully Oracle will improve the G1 garbage collector with subsequent releases.
感受学习的力量!