Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java CPU usage should be 100%... but it's not

I'm running a Java program on my Core i7 laptop which has 8 cores (4 physical, 4 HT). The program uses 8 parallel threads and so it should use up all the CPU. When running with the '-server' parameter, it is at 100% all the time. Without it, it's around 50%-60% overall (always changing with peaks at 100% and dips at 30%). Here's what I find weird: when I run the program in debug and wait for a moment where CPU usage is especially low (30%) and then suspend execution to look at what the eight threads are doing, none of them are in a blocking state. Furthermore, there's are almost no synchronization between them. Here's what I'm wondering:

  1. What's the difference between server and client VM that would prevent the CPU to reach 100% in client?
  2. In the absence of synchronization, what could be keeping a thread from using up a core fully? (probably linked to 1)

Edit: Here's a thought: the code allocates big arrays and leaves them to GC pretty quickly. Does a thread sleep when calling 'new SomethingBig()' and allocating that memory takes time? If there is a single process in the VM handling allocations for a bunch of threads, I guess that could explain why they seem to pause at random, outside of synchronization blocks...

Edit2: I'm pretty sure it is caused by GC now. The CPU reaches 100% again if I give the VM 1500Mb instead of the default 500Mb. I think the slowdown doesn't happen in server mode because it uses more memory by default.

like image 941
schmop Avatar asked Feb 15 '13 11:02

schmop


1 Answers

Not related to Java specifically, but the number of threads you have should be more than the number of core you have to make best use of your CPU, this is because there are plenty times when a particular thread can not execute, as well as the more obvious waiting on a condition variable held by another thread, threads can be block for other reasons too like a cache miss could require the thread to wait will data is loaded into the cpus cache, it could have to wait for input from some source that is slower than the cpu, virtually memory paging could cause your thread to wait. By have more threads than you have core, when one thread is blocked for what ever reason, another can make use the that core that is freed up. A good initial value is about 1.5 X the number of cores you have.

like image 54
Nathan Day Avatar answered Oct 24 '22 02:10

Nathan Day