Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to force an existing Java application to use no more than x cores?

We are benchmarking existing Java programs. They are threaded applications designed to benefit from multi-core CPUs. We would like to measure the effect of the number of cores on the running speed, but we are unwilling (and unable) to change the code of these applications.

Of course, we could test the software on different machines, but this is expensive and complicated. We would rather have a software solution.

Note: you can assume that the testing platform is either Windows, Linux or Mac. Ideally, we would like to be able to run the tests on either of these platforms.

like image 345
Daniel Lemire Avatar asked Jan 16 '12 16:01

Daniel Lemire


People also ask

Can Java use more than one core?

You can make use of multiple cores using multiple threads. But using a higher number of threads than the number of cores present in a machine can simply be a waste of resources. You can use availableProcessors() to get the number of cores. In Java 7 there is fork/join framework to make use of multiple cores.


1 Answers

It's called setting CPU affinity, and it's an OS setting for processes, not specific to Java.

On Linux: http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html

On Windows: http://www.addictivetips.com/windows-tips/how-to-set-processor-affinity-to-an-application-in-windows/

On Mac it doesn't look like you can set it: https://superuser.com/questions/149312/how-to-set-processor-affinity-on-os-x

like image 178
Chris Shain Avatar answered Oct 05 '22 23:10

Chris Shain