Join Our Telegram GroupsTelegram

Let's go over the steps on how to fix high CPU usage

Use cpulimit to free up your CPU
The recommended tool for managing system resources on Linux systems  . Although very powerful in terms of adjustable limits (CPU, memory, disk I/O, network, etc.), configuring cgroups is not simple. The nice  command has been available since 1973. But it only adjusts the scheduling priority between processes competing for time on a processor. nice The command does not limit the percentage of CPU cycles that a process can consume per unit of time. The cpulimit  command provides the best solution for both worlds. It limits the percentage of CPU cycles that a process can allocate per unit of time, and it is relatively easy to call.

cpulimit Commands are mainly useful for long-running and CPU-intensive processes. Compiling software and converting videos are common examples of long-running processes that can maximize the computer's CPU usage. Limiting the CPU usage of such processes will free up processor time for other tasks that may be running on the computer. Limiting CPU-intensive processes will also reduce power consumption and heat output, and may reduce system fan noise. The price of limiting the CPU usage of a process is that it takes more time to complete the run.

Install cpulimit

cpulimit The commands are available in the default Fedora Linux repository. Run the following command to install on Fedora Linux system  cpulimit:

  1. $ sudo dnf install cpulimit

View the documentation of cpulimit

cpulimit There is no man page attached to the package. Use the following command to view  cpulimit the built-in documentation. The output results are provided below. But you may need to run this command on your own system to prevent the options from changing since this article was written.

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


In order to demonstrate  cpulimit the use of commands, a well-designed and computationally intensive Python script is provided below. The script first runs with no limit, and then runs with a limit of 50%. It calculates the value of the 42nd  Fibonacci number  . The script time runs as a subprocess of the command in both cases  to show the total time required to calculate the answer.

  1. $ /bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'
  2. 267914296 (computed in 51.80 seconds)
  3. $ /bin/cpulimit -i -l 50 /bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'
  4. 267914296 (computed in 127.38 seconds)

When running the first version of the command, you may hear the CPU fan on the computer spin up. But when you run the second version, you shouldn't. The first version of the command is not limited by the CPU, but it should not cause your computer to be paralyzed. It is written in such a way that it can only use at most one CPU core. Most modern PCs have multiple CPU cores, and when one of the CPUs is 100% busy, other tasks can be run at the same time without difficulty. To verify whether the first command maximizes one of your processors, run the top command in a separate terminal window  and press the  1 key. To exit the  top command can press the  Q key.

Setting a limit higher than 100% only  makes sense for programs that can  parallelize tasks . For such a program, an increment higher than 100% represents the full utilization of a CPU (200%=2 CPUs, 300%=3 CPUs, etc.).

Note that in the above example, the -i options have been passed to the  cpulimit command. This is necessary because the command to be restricted is not  cpulimit a direct child of the command. Instead, it is time a subprocess of the  command, which in turn is cpulimit a subprocess of the  command. If there are no  -i options, cpulimit only time commands will be restricted  .

Final note

If you want to restrict a graphics program that is launched from the desktop icon, please  copy the program's  .desktop files (usually located in the  /usr/share/applicationsdirectory) to your  ~/.local/share/applications directory and modify the  Exec line accordingly. Then run the following command to apply these changes:

  1. $ update-desktop-database ~/.local/share/applications


Post a Comment

Hope you enjoyed the article!😊
Post a Comment