Reserve the NVIDIA GH200 Grace Hopper™ Superchip at Vultr today.
Author: Josh AmataLast Updated: Thu, Dec 23, 2021
This guide explains how to use
htop and manage memory usage on Linux with these utilities. To follow the steps in this guide, you should have a Linux system with
top (table of processes) is a command-line utility on Linux and other Unix-like operating systems that displays memory utilization and memory usage. It provides a dynamic real-time view of an active system. The top utility also displays system summary information, including the total number of tasks, percentage of CPU usage, and a list of processes or threads currently being managed by the Linux kernel.
The types of system summary information shown and the types, order, and size of information displayed for processes are all user-configurable, and that configuration can be made persistent across restarts.
By default, top outputs an ordered list of running processes based on CPU usage and shows the top CPU consumers, but the ordering criteria can be changed based on user needs. This list is updated periodically every 3 seconds by default, but the refresh period can be changed.
top is a handy tool for system administrators, as it shows useful information like what users and processes are consuming the most system resources at a given time.
To run the top utility, enter the top command into the terminal:
This starts an interactive terminal session. You should get an output similar to this:
top - 17:57:19 up 2:49, 3 users, load average: 1.39, 0.95, 0.80 Tasks: 246 total, 1 running, 245 sleeping, 0 stopped, 0 zombie %Cpu(s): 11.2 us, 3.8 sy, 0.0 ni, 83.0 id, 0.1 wa, 0.0 hi, 1.8 si, 0.0 st MiB Mem : 9866.1 total, 3699.1 free, 2174.4 used, 3992.6 buff/cache MiB Swap: 0.0 total, 0.0 free, 0.0 used. 6974.0 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2387 zero 20 0 3732904 399188 181584 S 21.5 4.0 11:13.31 firefox 2520 zero 20 0 3016704 369584 128644 S 13.2 3.7 4:50.27 Web Content 2575 zero 20 0 32.5g 340524 95436 S 7.9 3.4 1:11.20 WebExtensions 1190 root 20 0 769212 63116 32128 S 5.3 0.6 2:12.01 Xorg 1913 zero 20 0 3186144 148252 83092 S 4.6 1.5 2:02.33 kwin_x11 1703 zero 9 -11 2207532 30556 23860 S 3.3 0.3 1:16.19 pulseaudio 5004 zero 20 0 2033300 113144 65284 S 3.3 1.1 1:11.14 mpv 1980 zero 20 0 2284564 286600 110784 S 2.3 2.8 0:43.49 plasmashell 6230 zero 20 0 931792 77696 57568 S 1.0 0.8 0:04.42 konsole 1536 root 20 0 312580 121228 22764 S 0.7 1.2 0:12.81 faraday-server 658 root 0 -20 0 0 0 I 0.3 0.0 0:03.19 kworker/u9:2-i915_flip 2069 zero 20 0 3893108 116008 64028 S 0.3 1.1 0:12.44 java 2206 zero 20 0 366680 47956 42404 S 0.3 0.5 0:00.63 akonadi_akonote 2610 zero 20 0 2448256 130328 96260 S 0.3 1.3 0:05.78 Privileged Cont 7804 zero 20 0 12128 4840 3976 R 0.3 0.0 0:01.72 top 1 root 20 0 164304 10996 8000 S 0.0 0.1 0:03.50 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp 4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp 6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-events_highpri 9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq 10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_rude_
The upper half of the output contains statistics on resource usage and processes, while the lower half contains a list of running processes.
Let's break down the output, bit by bit. The first line shows the current timestamp (17:57:19), followed by the system uptime (i.e how long the system has been running - in this case, 2hrs and 49mins).
top - 17:57:19 up 2:49, 3 users, load average: 1.39, 0.95, 0.80
Next is the number of active user sessions (3), followed by the system's load average over the last 1, 5, and 15 minutes. Load refers to a measure of the amount of computational work the system performs. The load average is divided over 100, here the load average of our system in the last one minute is 1.39, which means the system was doing 139% of the available capacity. In this case, the system was a bit overloaded.
Tasks: 246 total, 1 running, 245 sleeping, 0 stopped, 0 zombie
The line shown above displays the total number of running tasks or processes. We have a total of 246 running tasks with 1 running, 245 sleeping, 0 stopped, and 0 zombies.
Running - these tasks refer to processes actively executing in the CPU or awaiting execution on the ready queue.
Sleeping - processes in this state are waiting for an event or I/O operation to be complete.
Stopped - these processes have been stopped by a job control signal.
Zombie - parent processes usually spawn child processes - which may be executed early, but their status has not been claimed by the parent process that created them; so the child processes, although they may be terminated, still have an entry in the process table. These kinds of processes are called zombie processes.
The next line shows CPU usage in percentage. This tells how much CPU time is spent on various tasks.
%Cpu(s): 11.2 us, 3.8 sy, 0.0 ni, 83.0 id, 0.1 wa, 0.0 hi, 1.8 si, 0.0 st
The symbols have the following meaning:
us (user space) - time spent executing processes in the user-space.
sy (kernel space) - time spent executing processes in the kernel space.
ni (niceness) - Linux determines the priority of processes using nice values ranging from -20 to +19. The lower the nice value of a process, the higher the priority (i.e -20 signifies highest priority and +19, lowest priority). By default, processes have a nice value of 0, although this can be changed. This column specifies the percentage of CPU time spent executing processes with a manually set nice value.
id (idle) - this represents the time the CPU spends idle.
wa (wait) - the amount of time the CPU spends waiting for I/O to complete.
hi (hardware interrupt) - time spent handling hardware interrupts.
si (software interrupt) - time spent handling software interrupts.
st (steal time) - in virtual environments, CPU resources are shared to each virtual machine - when the OS has work to do but can't because the CPU is busy on another VM - the amount of time lost is called steal time.
These lines represent physical memory (RAM) usage in mebibytes(MiB):
MiB Mem : 9866.1 total, 3699.1 free, 2174.4 used, 3992.6 buff/cache MiB Swap: 0.0 total, 0.0 free, 0.0 used. 6974.0 avail Mem
The first line shows our total memory usage, free, used, and cached memory, while the second displays similar information about swap memory.
The columns in the second half of the output display specific information about processes.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
PID (Process ID) - Every process is assigned an ID. This column contains a positive integer that identifies the unique ID of a process.
USER - This holds the effective username of the user who started the process.
PR - This field displays the scheduling priority of the process from the kernel perspective. It can be calculated by adding +20 to the niceness value of the process. A value of "rt" in this field shows that a process is running under real-time scheduling.
NI - This field shows the "niceness" value of the process.
VIRT - This represents the virtual size of a process which is the total sum of memory a process can access at any moment, such as the memory it has mapped to itself, files on disk that have been mapped to it, and memory shared with other processes.
RES - This field indicates how much actual physical memory a process is consuming and also corresponds to the %MEM field. It stands for resident size.
SHR - This indicates the amount of memory shared with other processes.
S - This field shows the state of a process in single-letter form. The values it can take on include:
D - uninterruptible sleep.
I - idle.
R - running.
S - sleeping.
T - stopped by a job control signal.
t - stopped by debugger during a trace.
Z - zombie.
%CPU - The amount of CPU time used by the process since the last update.
%MEM - Percentage of physical memory used by the process.
TIME+ - This field holds the total CPU time used by the process since it started and has a precision to the hundredths of a second.
COMMAND - This column shows the name of the process.
Now that we've gone through the output let's dive into managing processes.
Rhave two different effects.
By default, top has a refresh interval of 3 seconds. To change this interval - press
d; you will be prompted for the new interval to set in seconds:
Change delay from 3.0 to _
To kill a process, press
k while top is running, you'll be presented a prompt like this within top:
PID to signal/kill [default pid = 2387] _
Enter the PID of the process you wish to kill; let's kill firefox (PID=2387) from our top:
PID to signal/kill [default pid = 2387] 2387
Next, you'll be prompted to enter the kill signal to be sent to the process. By default, top sends a SIGTERM, which allows processes to be terminated gracefully. You could type in SIGKILL or another signal you wish to send; you could also enter a signal number here. For instance, SIGKILL has a signal number of 9, and SIGTERM is 15.
In our example, memory usage is given in mebibytes (MiB):
MiB Mem : 9866.1 total, 3699.1 free, 2174.4 used, 3992.6 buff/cache MiB Swap: 0.0 total, 0.0 free, 0.0 used. 6974.0 avail Mem
We can cycle between displaying memory values in kibibytes (KiB), mebibytes (MiB), gibibytes (GiB), tebibytes (TiB), pebibytes (PiB), or exibibytes (EiB) by using
E to switch within top.
To switch between similar memory units for the process list, an
e is used.
The output might feel too cluttered, especially when you want to monitor just a bunch of similarly named processes or a single process. You can add filters to focus on just a few processes.
To use filters, enter
O in top; a prompt will appear, and you can add a filter expression here:
add filter #1 (ignoring case) as: [!]FLD?VAL _
A filter expression specifies a relation between an attribute and a value. Examples of filter expressions are:
COMMAND=somescript - filters processes containing "somescript" in the COMMAND attribute.
!COMMAND=somescript - filters processes that do not have "somescript" in the command attribute.
%CPU>2.0 - filters processes that have a CPU utilization of more than 2.0%.
You can add more filters even if you've already used one; this helps to further trim down to your desired result. To remove any filters you have added, enter
You can sort the top's process list by CPU utilization, memory use, etc.
P, and it will be sorted according to CPU utilization percentage in descending order, with processes with more CPU utilization listed first.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1703 zero 9 -11 2207532 30556 23860 S 5.3 0.3 1:23.75 pulseaudio 5004 zero 20 0 2033300 113672 65284 S 4.3 1.1 1:18.77 mpv 1190 root 20 0 769176 63116 32128 S 2.3 0.6 2:23.67 Xorg 2387 zero 20 0 3731612 405248 180520 S 1.3 4.0 11:54.52 firefox 2520 zero 20 0 3026756 430768 126412 S 1.0 4.3 5:38.60 Web Content 1164 root 20 0 1684460 42536 7124 S 0.7 0.4 0:18.04 opensnitchd 1980 zero 20 0 2284556 286600 110784 S 0.7 2.8 0:45.47 plasmashell 9157 zero 20 0 12128 4792 3932 R 0.7 0.0 0:00.07 top 1 root 20 0 164304 10996 8000 S 0.3 0.1 0:03.56 systemd 1157 redis 20 0 67244 15284 8368 S 0.3 0.2 0:10.98 redis-server
You can also press
M, to sort by memory usage;
T to sort by the TIME+ column, and
N to sort by the PID column.
top also allows you to sort by other different attributes like VIRT, PR, NI. To select from a list of attributes to sort by, follow these steps while in top:
F to enter the interactive menu.
Use the arrow keys to navigate the list of attributes to sort by.
s to select your choice.
Press ENTER to save your selection.
q to exit the interactive menu.
This will order the process list based on your selected attribute. By default, top sorts in DESC (descending order), to switch between AESC (ascending order) and DESC; press
Alternatively, you can launch top with the
o flag followed by the attribute to sort by, like "top -o attribute". For example, to sort by %CPU; we can launch top like this:
$ top -o %CPU
You can view processes for a specific user by pressing
u, this will prompt for the user name of UID:
Which user (blank for all) _
Enter the user name or UID and hit enter. Alternatively, you can launch top with the
u flag followed by the user name:
$ top -u user
top allows you to renice a process by pressing
r; it then prompts you for the PID of the process you wish to renice:
PID to renice [default pid =5004] _
Enter the process ID and enter, you'll be prompted for the new nice value to assign to the process. Nice values range from -20 to +19.
You can switch top's default way of showing CPU and memory statistics.
top allows cycling between four different views. To switch between different CPU and memory views, press
Pressing these keys have the following effect on top's display:
H - switch tops display to threads instead of processes. Alternatively, you can launch top with the display set to threads by default with the H flag.
$ top -H
c - shows the full path to the program in the COMMAND column. Alternatively, you can launch top with the c flag.
$ top -c
V - switches to forest view, which shows a child-parent hierarchy of processes.
Space - this forces top to refresh its display when pressed.
To save any changes made to top's output for use later on, pressing
W writes the configuration to the .toprc file in your home directory.
htop is an interactive process viewer and process manager like top, but it is not installed on most Linux distributions by default - unlike top.
To install htop:
$ sudo apt install htop
While top focuses on the processes that consume the most system resources, htop shows all running processes.
htop also provides a rich-colored display and visual information about processor, swap, and memory status. It also provides a cursor-controlled interface for sending signals to processes. htop includes similar information as top, such as same columns information in the process list, but ties behavior to function keys rather than key presses used in top. To perform an action on a process, navigate to the process using the arrow keys and clicking the following function keys:
Help (F1) - opens htopâs help page.
Setup (F2) - opens the setup page to configure the htop system statistics display.
Search (F3) - this will present a prompt to enter the name of the process you want to find. If found, the process will be highlighted.
Filter (F4) - prompt for a name to filter the output.
Tree (F5) - Shows the hierarchical relationship between processes.
Sort by (F6) - spawns a menu by the side where you can select what attribute to sort the output by.
Niceness (F7 and F8) - decrement or increment niceness with F7 and F8 respectively on the highlighted process.
Kill a process (F9) - this will spawn a menu by the side, where you can select what termination signal to send to the highlighted process.
Quit (F10) - exit the htop session.
htop has similar functionality to top, but many system administrators prefer htop over top, due to its rich display and ease of use.