Linux Check Memory Usage: A Comprehensive Guide

linux check memory usage

As a Linux user, you must be aware of the importance of monitoring system resources such as memory usage. Memory is a crucial component of your system, and if it is not managed properly, it can lead to system crashes, poor performance, and other issues. In this article, we will discuss how to check memory usage in Linux.

What is Memory Usage?

Memory usage refers to the amount of memory that is currently being used by the system. In Linux, memory is divided into two categories: physical memory and virtual memory. Physical memory is the actual RAM installed on your system, while virtual memory is a combination of RAM and swap space (hard disk space used as an extension of RAM).

When your system runs out of physical memory, it starts using virtual memory. However, using too much virtual memory can slow down your system because hard disk access is slower than RAM access.

Checking Memory Usage in Linux

There are several tools available in Linux to check memory usage. In this section, we will discuss some commonly used tools.

1. free

The free command is used to display the amount of free and used memory in the system. To use this command, open the terminal and type the following command:

free -h

The -h option is used to display the memory usage in a human-readable format. The output will look like this:

              total        used        free      shared  buff/cache   available
Mem:           7.8G        1.6G        4.4G        120M        1.8G        5.9G
Swap:          2.0G          0B        2.0G

The Mem column shows the total amount of physical memory, the amount of memory used, and the amount of free memory. The Swap column shows the total amount of swap space, the amount of swap space used, and the amount of free swap space.

2. top

The top command is used to display real-time information about the system, including memory usage. To use this command, open the terminal and type the following command:

top

The output will look like this:

top - 11:17:18 up  1:30,  1 user,  load average: 0.00, 0.02, 0.00
Tasks: 181 total,   1 running, 180 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8167592 total,  4411408 free,  1932480 used,  1821704 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  5472000 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                    
 2771 root      20   0  551520  42984  34584 S   0.0  0.5   0:00.76 Xorg                                                                                                       
 3077 user      20   0  888456  77228  47552 S   0.0  0.9   0:08.89 gnome-shell                                                                                                
 3193 user      20   0  495032  32928  26756 S   0.0  0.4   0:01.70 gnome-terminal-                                                                                             
 3199 user      20   0   31608   3728   3208 R   0.0  0.0   0:00.03 top                                                                                                        
    1 root      20   0  225288  14508   9328 S   0.0  0.2   0:01.54 systemd                                                                                                    
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd                                                                                                   
    3 root       0   0       0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/0                                                                                                
    5 root       0   0       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                                                                               

The KiB Mem line shows the total amount of physical memory, the amount of memory used, and the amount of free memory. The KiB Swap line shows the total amount of swap space, the amount of swap space used, and the amount of free swap space. The %MEM column shows the percentage of memory used by each process.

3. htop

The htop command is a more advanced version of the top command. It provides real-time information about the system, including memory usage, in a more user-friendly interface. To use this command, open the terminal and type the following command:

htop

The output will look like this:

htop

The MEM% column shows the percentage of memory used by each process.

Conclusion

Checking memory usage is an essential task for any Linux user. In this article, we discussed some commonly used tools to check memory usage, including free, top, and htop. By using these tools, you can monitor your system’s memory usage and ensure that it is managed properly.