summary
-
All processes on Linux require RAM and CPU cycles, and competition for these resources can occur.
-
Tools such as free, top, and htop can help you identify large memory hogs and high CPU usage.
-
Use commands such as ps and top to identify and kill problematic processes to restore system performance.
All code running inside a Linux computer requires RAM and CPU cycles. When a process uses more than its fair share, it slows down other processes. Here’s how to find the culprit.
The act of balancing processes and resources
rum and CPU Cycles are a finite resource. When a program’s code is executed, a process is formed. In addition to the operating system processes, there are also processes that run the desktop environment and the commands and applications that it launches. All of these require CPU time and RAM.
Linux and the CPU must manage RAM allocation, balance CPU workload across cores and threads, and schedule it to ensure that all processes share it.
Applications are supposed to be written so that they don’t hog the machine, but sometimes problems can occur where processes hog all the RAM and try to monopolize the CPU.
At this time, it is necessary to be able to identify the runway process.
Investigating high RAM usage
Linux uses free RAM as cache. Although it may appear that all your RAM is being used, that is probably a false impression. If necessary, items are removed from the cache and RAM is allocated to regular processes.
free command
With the free command, Memory Usage Snapshot Summary. The -h (human) option makes the output more readable.
I ran the command three times in a terminal window. In a separate terminal window, I ran a program that required a large amount of RAM.
free -h
free -h
free -h
Free RAM decreases at an alarming rate. You need to identify the process behind it.
Reading /proc/meminfo
Tools such as top and htop display memory information. /proc/meminfo pseudo file. /proc/meminfo behaves like a file, so you can examine its contents using common utilities such as cat and less.
less /proc/meminfo
The output varies depending on the running kernel and CPU architecture, but the standard fields are always present. This is a good way to see in more detail what type of memory usage is taking up most of your RAM, but it doesn’t identify individual processes.
Discover memory footprint using the vmstat command
Using the vmstat command: Changes in virtual memory usage.
To display four result sets containing values ​​displayed in MiB at 5 second intervals, use the following command:
vmstat 5 4 -S M
Detect high memory consumption using top and htop
both top and h top Provides a dynamic dashboard of system information with a process table that shows one row of information for each process.
To sort the process table by RAM usage, press Shift+M.
A program called gobble was started twice. It’s using 2/3 of the RAM. Kill these two processes.
Press k to start the kill function.
At the top you need to specify the process ID of the process you want to kill. By default, the kill function uses the process ID from the first line. Since we’ve sorted the table by RAM usage, that will be the ID we want to use, so we can just press Enter.
You will be asked which signals should be sent to the process. SIGKILL is signal number 9. You must type the number 9 and press Enter again.
Repeating this process twice will terminate both instances of the gobble program.
htop is very similar. Navigate to the process you want to kill the highlight bar. Press k to start the force quit process. A list of signals will appear on the left side of the window.
Move the small highlight bar to the 9 SIGKILL entry and press Enter.
Find high memory consumption using the ps command
of ps command You will get the process ID. Also, parent Process ID. If there are many memory-hungry processes started by the same parent process, terminating the parent process will also terminate its child processes.
Use the -e (all processes) and -o (user-defined output formats) options. In the output I’m looking for:
- Pido: Process ID.
- pid: Parent process ID.
- communication: Command name.
- %mem: Percentage of RAM used by this process.
- RSS: Size of resident set. This is the unswapped physical memory (in kilobytes) used by the process. Note that reserved memory or virtual memory that is not actually used is not counted. However, for the task at hand, this is a perfectly good indicator.
- %CPU: This is the process’s previous CPU time divided by the process’s execution time.
Organized based on memory. A minus sign means reverse sort. By piping the results through head we can get the 10 worst offenders.
ps -e -o pid,ppid,comm,%mem,rss,%cpu --sort=-%mem | head -10
Investigate high CPU usage
The steps to track CPU load are very similar to those described for memory load. You need to identify the offending process and get its process ID.
Detect CPU usage using top and htop
You can use top and htop in much the same way as you would to track memory-intensive processes. By default, top and htop sort the processes table by the %CPU column. If you change the sort column, you can restore sorting by %CPU by pressing Shift+P (for processors).
There is a process called Drain that itself consumes almost all of the CPU time. Once you have identified the culprit, you can press the k key to invoke the force quit function.
Monitor CPU usage using the mpstat command
Be careful when killing processes. Some processes, especially system processes, should be left alone. The mpstat command provides a snapshot of CPU usage for user and system processes.
Fedora and Manjaro require mpstat to be installed, but Ubuntu PCs already have it installed.
On Fedora, you need to type:
sudo dnf install sysstat
In Manjaro, the command is:
sudo pacman -S sysstat
Use the -P all (all processors) option to request a total of 5 reports every 2 seconds.
mpstat -P all 2 5
You can see that the load on the CPU is coming from the user side, not the system side. The sixth row is the average of the five requested reports.
Detect CPU occupancy using ps
You can also use ps to find processes with high CPU load. With a few tweaks to the previous command, you can sort by CPU.
ps -e -o pid,ppid,comm,%mem,rss,%cpu --sort=-%cpu | head -10
I know the entry about ps was a false flag and only ran for a split second. The second line displays the process ID of the process hogging the CPU. With this information, you can terminate the process using top or htop or the kill command.
a little housework
Most of the time, Linux computers work fine and all processes operate within acceptable limits. If you notice that your computer is running slowly, use the following tools to investigate. It can be memory and CPU intensive.