I have a java servlet application on Java 7 that is normally very healthy in terms of system resource consumption. Normally CPU usage on the server is below 50%. However in the few minutes following startup it behaves much differently, to the point that the CPU can become pegged at 100% for several minutes if its trying to serve a lot of traffic during that period. The result is slow response times, network timeouts, and even long garbage collection pauses sometimes.
To diagnose the issue, I took a series of thread dumps while the server was starting up and I ran top -H at the same time. By matching each java thread to a pid, I can consistently see the C2 CompilerThread
using by far the most CPU. I have done research about what this thread does and I understand that it is a Java compiler optimizing code based on runtime statistics. But from all the reading I've done, I can't tell the best approach to making the situation better. The only options I can glean are:
What is the best approach to take and are there other options to try and alleviate CPU usage after startup?
There are several paid-for JVM technology available to alleviate your problem, using ahead-of-time compilation.
However, if you want to stick to the standard JVMs, one trick people use is after startup you send a few dummy requests so that the JVM is warmed up before your actual operation starts. That way you can decide when you want to pay for the JVM warm-up cost before serving your customers.
You can also force the JVM to compile all code by -Xcomp command line options for hotspot, but we don't recommend it as it will slow down the start up of your application by compiling seldomly used code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With