This question already has an answer here:

I have some binary I want to run in terminal, but if I run it, it quickly starts to use 100% of both CPUs, 100% of memory and the whole Linux freeze in a few seconds. Also I don't have source (in order to calm it down right from the inside). I googled a lot and found and tried two possible solutions:

  1. nice - does literally nothing, the binary still uses 100% of both CPUs and 100% of memory quickly after I run it
  2. ulimit -v makes binary and even some other commands (like git) crash and throw some error

Is there any easy solution to limit CPU usage or memory by this binary and let it use e.g. only one CPU or 50% of both CPUs or only some small amount memory? I imagine something like this:

user@linux:~$ magical_command --limitmemory 1M --command mybinary


user@linux:~$ magical_command --cpu both --limitcpu 50% --command mybinary


user@linux:~$ magical_command --cpu one --limitcpu 100% --command mybinary

Update: From the comments below it looks like it is caused by memory rather than CPU overload, therefore I edited the question, but I still recommend @dbkeys' answer for those looking for CPU limiting magical command.

Update 2: As requested in the comments below: if I restart my notebook and only run terminal and that binary from terminal, nothing else, my notebook is significantly slowed down, mouse and keyboard works with delays or jerkily. But the binary is working for hours (most of time downloading and compiling dependencies), therefore I open Chrome and do some other stuff. If I open Chrome and let the binary run in the background, the whole linux freezes at all and nothing, even not mouse responds. I have to push a switch-off buton for 5 seconds to hard shutdown. But I can't just run it and go away, I want to work on some other stuff, therefore I want to limit somehow maybe both, that binary and Chrome.

marked as duplicate by muru, heemayl bash Oct 24 '16 at 14:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you sure the problem is the CPU usage and not the memory usage? – terdon Oct 24 '16 at 12:38
  • What CPU and how much RAM do you have? – negusp Oct 24 '16 at 12:41
  • You can use cgroups to limit memory usage and restrict a program to a set of cores. See, for example: askubuntu.com/a/510936/158442 – muru Oct 24 '16 at 12:43
  • @terdon maybe, I don't have any time to find out. What I only managed to do was that I run System monitoring and looked how my PC freezes real-time: usual usage (Google Chrome and clean terminal without any command running): cca 10% CPU1; cca 10% CPU2; cca 75% memory... Google Chrome and terminal running that binary: 100% CPU1; 100% CPU2; 100% memory – aleskva Oct 24 '16 at 12:57
  • 1
    The problem then is almost certainly the memory usage, not the CPU. nice would solve the CPU. It sounds like a buggy program. You could also try allocating more swap to see if that helps. – terdon Oct 24 '16 at 13:06

taskset, might be the 'magical_command' you are looking for. According to its man page:

"...taskset is used to set or retrieve the CPU affinity of a running process given its pid, or to launch a new command with a given CPU affinity. CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. "

This would limit mybinary to run on the first two cores:

taskset --cpu-list 0,1 mybinary

Unfortunately, I don't see a parameter in taskset that would also limit what % of the CPU's time you want to allot it to run mybinary.

  • Well, this is very relevant to what OP asked but he is actually RAM constrained. Good answer though. – negusp Oct 24 '16 at 13:20
  • @dbkeys I edited the question, but still tried to lead people looking for CPU overload to your great answer – aleskva Oct 24 '16 at 13:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.