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:
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.
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.
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