What are the commands for viewing memory usage in linux

The commands include: 1. The free command can display the system memory status, including the usage of physical memory, shared memory and system cache; 2. The “cat /proc/meminfo” command can read the “/proc/meminfo” file Content, display memory usage; 3, vmstat command, can be used to monitor CPU usage, process status and other information; 4, top command; 5, htop command; 6, sar command; 7, smem command; 8, glances command; 9, ps_mem command.

The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

We often need to check the memory usage of the server and the memory occupied by each process to avoid resource shortages and affect user experience.

For websites and web services, it is necessary to have sufficient memory to handle client requests. If there is not enough memory, it will become slow, congested, and even service crashes during peak request times. Of course, the same is true for desktop systems.

Memory management and optimization is an important part of Linux system performance optimization. In other words, whether memory resources are sufficient will directly affect the performance of application systems (including operating systems and applications).

Here are a few system commands, through which you can quickly check the memory usage in the Linux system.

  1. Use the free command

The free command is the simplest and most commonly used memory viewing command in the Linux system.

The free command is used to display the system memory status, including the usage of system physical memory, virtual memory (swap swap partition), shared memory and system cache

Examples are as follows:

free -m

1.png

free -h

2.png

Among them, the -m option displays memory usage information in MB; the -h option displays it in human-readable units.

In the example above, the Mem: line:

total means that there is a total of 7822MB of physical memory (RAM), which is 7.6G.
used indicates the usage of physical memory, which is about 322M.
free means free memory;
shared means shared memory?;
buff/cache indicates the amount of cache and buffer memory; the Linux system will cache a lot of things to improve performance, and this part of memory can be released when necessary for other programs to use.
available means available memory;

The output is easy to understand. The line Swap indicates swap memory, as can be seen from the numbers in the example, basically no swap memory is used.

  1. View /proc/meminfo

Another way is to read the /proc/meminfo file. We know that the /proc directory is full of virtual files, which contain dynamic information related to the kernel and operating system.

$ cat /proc/meminfo

MemTotal: 8010408 kB

MemFree: 323424 kB

MemAvailable: 6956280 kB

Buffers: 719620 kB

Cached: 5817644 kB

SwapCached: 132 kB

Active: 5415824 kB

Inactive: 1369528 kB

Active(anon): 385660 kB

Inactive(anon): 249292 kB

Active(file): 5030164 kB

Inactive(file): 1120236 kB

Unevictable: 0 kB

Mlocked: 0 kB

SwapTotal: 4194304 kB

SwapFree: 4193580 kB

Dirty: 60 kB

Writeback: 0 kB

AnonPages: 247888 kB

Mapped: 61728 kB

Shmem: 386864 kB

Slab: 818320 kB

SReclaimable: 788436 kB

SUnreclaim: 29884 kB

KernelStack: 2848 kB

Page Tables: 5780 kB

NFS_Unstable: 0 kB

Bounce: 0 kB

WritebackTmp: 0 kB

CommitLimit: 8199508 kB

Committed_AS: 942596 kB

VmallocTotal: 34359738367 kB

VmallocUsed: 22528 kB

VmallocChunk: 34359707388 kB

HardwareCorrupted: 0 kB

AnonHugePages: 88064 kB

HugePages_Total: 0

HugePages_Free: 0

HugePages_Rsvd: 0

HugePages_Surp: 0

Hugepagesize: 2048 kB

DirectMap4k: 176000 kB

DirectMap2M: 6115328 kB

DirectMap1G: 4194304 kB

Focus on these data:

MemTotal, total memory
MemFree, free memory
MemAvailable, available memory
Buffers
Cached, cache
SwapTotal, swap memory
SwapFree, free swap memory

The information provided is similar to what the free command sees.

  1. Use the vmstat command

The vmstat command is the abbreviation of Virtual Meomory Statistics (Virtual Memory Statistics), which can be used to monitor CPU usage, process status, memory usage, virtual memory usage, hard disk input/output status and other information.

Using the vmstat -s command and options, statistics on memory usage can be performed, similar to /proc/meminfo.

Examples are as follows:

vmstat -s

3.png

The first few lines show information such as total memory, usage, and free memory.

  1. use the top command

The top command is generally used to view the CPU and memory usage of a process; of course, it also reports the total amount of memory and memory usage, so it can be used to monitor the usage of physical memory.
Summary information is displayed at the top of the output.

Example output:

top – 15:20:30 up 6:57, 5 users, load average: 0.64, 0.44, 0.33

Tasks: 265 total, 1 running, 263 sleeping, 0 stopped, 1 zombie

%Cpu(s): 7.8 us, 2.4 sy, 0.0 ni, 88.9 id, 0.9 wa, 0.0 hi, 0.0 si, 0.0 st

KiB Mem: 8167848 total, 6642360 used, 1525488 free, 1026876 buffers

KiB Swap: 1998844 total, 0 used, 1998844 free, 2138148 cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME + COMMAND

2986 enlightene 20 0 584m 42m 26m S 14.3 0.5 0:44.27 yakuake

1305 root 20 0 448m 68m 39m S 5.0 0.9 3:33.98 Xorg

7701 enlightene 20 0 424m 17m 10m S 4.0 0.2 0:00.12 kio_thumbnail

The parameters provided by various operating systems are slightly different, and generally speaking, they can be sorted according to CPU and memory.

For example:

CentOS

top -o %MEM

top -o %CPU

mac

top -o mem

top -o cpu

If you encounter something unclear, please use top -h to view the help information.

Focus on the KiB Mem and KiB Swap lines at the top. Indicates the total amount of memory, the amount used, and the amount available.
The buffer and cache parts are similar to those shown by the free command.

  1. Use the htop command

The htop command is an interactive process viewer for Linux/Unix systems. It is a text-mode application and requires the ncurses library, which was developed by Hisham. It is designed as an alternative to top-level commands. This is similar to the top command, but allows you to scroll vertically and horizontally to see all processes running on the system. htop comes with Visual Colors which are an added bonus and very visible when tracking system performance. Freedom to perform any task related to a process, such as process termination and reset, without entering its PID.

Similar to the top command, but htop also displays various other information, and supports color display.

htop

4.png

The messages at the top show CPU usage, as well as RAM and swap memory usage.

If not installed, you can use a similar command:

yum install htop -y

  1. Use the sar command

The sar command can also be used to monitor the memory usage of Linux. Through the combination of “sar -r”, you can view the usage of system memory and swap space.

The sar command is very powerful and is one of the important tools for analyzing system performance. Through this command, you can comprehensively obtain the system's CPU, running queue, disk read and write (I/O), partition (swap area), memory, CPU Performance data such as outages and networking.

The following is the output of the “sar -r” command:

sar -r 2 3

5.png

In this output result, the meanings of each parameter are as follows:

kbmemfree: indicates the size of free physical memory;

kbmemeused: indicates the size of the physical memory used;

%memused: Indicates the percentage of used memory to total memory size;

kbbuffers: indicates the size of the physical memory used by the buffer;

kbcached: Indicates the size of the physical memory used by the cache;

kbcommit and %commit: respectively represent the memory size and percentage used by applications in the current system;

Compared with the free command, the output information of the sar command is more user-friendly. It not only gives the memory usage, but also gives the percentage of memory usage and the average value of statistics. For example, only one item of %commit can tell that the memory resources in the current system are sufficient.

7. Use the smem command

smem is a tool that provides reports on the memory usage of a large number of Linux systems. Unlike existing tools, smem can report proportional set size (PSS), unique set size (USS) and resident set size (RSS). Proportional Set Size (PSS): Refers to the amount of memory used by libraries and applications in the virtual memory system. Unique Set Size (USS): Unshared memory is reported as USS (Unique Set Size). Resident Set Size (RSS): A standard measure of physical memory (often shared among multiple applications) usage, known as Resident Set Size (RSS), will greatly overestimate memory usage. Note: If the prompt is not found after executing the following command, please execute “yum install smem” to install

smem-tk

6 (1).gif

8. Use the glances command

glances is a cross-platform system monitoring tool written in Python. All information such as CPU usage, memory usage, running processes, network interfaces, disk I/O, Raid, sensors, file system information, Docker, system information, uptime, etc. can be viewed.

glances

7.gif8.png

9. Use the ps_mem command

ps_mem is a simple Python script that allows you to accurately get the core memory usage of programs in Linux. This determines how much RAM is used per program (not per process). It calculates the total amount of memory used by each program, total = sum(private RAM for program processes) + sum(shared RAM for program processes). There are issues with calculating shared RAM, and the tool automatically chooses the most accurate method for the running core.

ps_mem

10.gif

Note: If you execute the following command prompt not found, please execute “yum install ps_mem” to install

Reprinted from: https://m.php.cn/faq/498484.html
Original Author: Blue Lantern Night Tour
Invasion