Search code examples
javacpucpu-usagesingle-threaded

How can CPU usage max out at 50% but not use one CPU effectively?


Suppose I have a java program that is thread-bound to one-CPU. From the left hand of the graph below - we can see one of the two CPUs is maxed out. (It has held this position for a while).

But the graph on the right shows that the different CPUs are doing different activities and one is not blocked.

enter image description here

My question is: How can CPU usage max out at 50% but not use one CPU effectively?


Solution

  • Even if your process only uses only one core / CPU Windows tries to distribute the load over all available cores. This happens so fast / often that it looks like no core is ever used to 100%.

    If you tell Windows to only use one core, you will see this one reach 100%. You can do this either by opening taskmgr and right clicking on the process and choosing affinity or when starting the application.

    I can show you how this is done with 7z because it does not matter which (single core) application you use, the effect is the same.

    start /affinity 0x1 7z b -mmt1 does start 7z and using /affinity 0x1 assigns the first core as the only core. (b -mmt1 is there to put 7-zip into benchmark mode which uses only one thread) If you launched the process regularly you can see / change the affinity from taskmgr. affinity of a process

    The CPU chart will in both cases show 100%:

    100% on a single core

    All screenshots above are from a German Windows 10, this is how it would look like on an English Windows 7: enter image description here