Prevent apps from consuming 100% of CPU

Hi, everyone.

I run heavy development workloads on Manjaro that consume high amounts of RAM (Docker, Android emulator, etc.) as well as high amounts of CPU (during compilation/build). I can deal with RAM by allocating more swap space, but I am not sure what to do about CPU. The problem is that sometimes some build tool goes out of control and it needs to be killed. But, since it consumes all cores at the same time, my Desktop (KDE) stops responding to input, leaving force reboot as the only option. On Windows 10 this wasn’t an issue as I could still open the Task Manager even when some tool hogged the CPU. So I was wondering if it was possible to somehow “reserve” a portion of the CPU to the system so I could always open a System Monitor if a certain application spins out of control.

sounds like your exceeding your specs, the simple fix is to just do less at once.

I guess the best option would be restricting the average load to a specific level. Meson build have it for example:

meson compile -l LOAD_AVERAGE

Look what your build tool has to offer.

Maybe you can also set a higher nice value for a programm:

nice --adjustment=N <program/command>
# OR
nice -nN <program/command>
  • -20 → highest priority
  • 19 → lowest priority

or maybe cpulimit could an option:

cpulimit -l 20 firefox

20 means, it is limted to 20% of the cpu load

Those seem like a great solution, but the problem is that it’s not as simple as configuring a single program, since during the aforementioned workloads a whole suite of tools is getting executed and it’s not trivial to determine which one of them needs to be restricted, because when some process hogs the CPU, I cannot open the Process Monitor, or any tool for that matter to diagnose which process is the culprit. And even if I knew, these processes are short-lived and spawned frequently, so it’s not scalable too. The only solution I see in this situation, thanks to your example, is to always keep a running instance of a Process Monitor with highest process priority.

Well, among other specs, Android Studio requires 8Gb RAM or more, as well as 2nd gen Intel Core or newer according to the official system requirements. My system has 16Gb RAM and 7th gen Core i7, which exceeds those requirements. But this still misses the point, because no matter how much computational capacity a system has, some build tools can easily consume all of it because of their highly parallelizable workloads.

I was going to suggest that, limit the CPU cycles with cpulimit, and also you can limit which core to use with taskset so you keep at least a core for the system to survive heavy load.

I think the child processes inherits the initial settings from these tools too, not sure about your specific workload but back in the days when using these tools on servers it was working like that I think.

//EDIT: also in /etc/security/limits.conf I think you can set niceness and things like that specifically for each programs.
//EDIT2: no you could set a maximum/minimum niceness for a user though. Then you would need to run your work under this specific limited user.

1 Like

I never thought of it this way, will definitely limit the top process (android-studio) and see how it performs. Thanks. Also thanks for the suggestion about limits.conf and user-specific niceness values. Will try them out as well if cpulimit doesn’t work.

By the way, I think it should be limitcpu, not cpulimit. From the description, I got the impression that cpulimit sends the termination signal to the process, while limitcpu actually throttles it.

[omano@omano-nvme ~]$ cpulimit -h
Usage: cpulimit [OPTIONS...] TARGET
      -l, --limit=N          percentage of cpu allowed from 0 to 1200 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

Report bugs to <>.

Yes apparently you can, with -i switch