Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C2 Compiler saturating CPU at startup

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:

  1. Switch from C2 to TieredCompiler (but will this result in better performance in the first few minutes after startup?)
  2. Turn on -XX:+PrintCompilation to see what is being optimized (but what do I do with this information? Can I force it to be optimized before the server is accepting traffic somehow?)

What is the best approach to take and are there other options to try and alleviate CPU usage after startup?

like image 420
Cameron Avatar asked Oct 07 '14 21:10

Cameron


1 Answers

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.

like image 160
sunnychan77 Avatar answered Oct 04 '22 23:10

sunnychan77