Take the 2-minute tour ×

I'm running JVM instances on a CentOS 6.3 x64 machine. Sometimes the CPU usage of one goes rampant and I/O blocks the entire machine.

How can I limit the usage of all Java processes to a single core? Or two cores? Or as specified?

I've tried cpulimit, which has failed to work for multiple cores, as well as multiple processes with the same name.

Is there a simple way to limit the CPU usage of all processes of a specified name?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Well, I think all you need is pgrep (to find the PIDs) and taskset. And then depending on your needs you'd run (as superuser) something like:

for pid in $(pgrep <pattern>);
  taskset -p 0x00000001 $pid

Consult the taskset manual page for more information on how to use it. You may need to install the schedutils package.

Of course you can vary that scheme to whatever complexity you desire, by modifying the processor mask (i.e. which CPU you assign).

I should add that I think cpuset (assuming you meant that, I'm not aware of a tool named cpulimit but that doesn't mean anything) uses the same underlying mechanisms. Therefore this may be in vain just as your past attempts.

share|improve this answer
I used taskset on an individual process but it continues to use up to 3 threads. Would you happen to know why? –  hexacyanide Feb 20 '13 at 0:44
@hexacyanide: nope, I don't because I don't know the exact program (and even if I did it might not make a difference). But threads are an "entity" inside a process. Indeed every process has a single thread at least (technically). The thread is what executes a "thread of instructions", figuratively speaking, whereas the process is the container providing memory space and so on. However, with taskset all of these threads would still be confined to using the CPU or CPUs you assigned in the processor mask ... –  0xC0000022L Feb 20 '13 at 0:46
add comment

Your Answer


