Check memory usage in Linux system

operating system



1. View memory usage

In the Linux system, most operations are done through the command line, because the graphical interface is not opened in most cases.
In a server environment, operations can only be performed through the shell.
The following describes the related commands to check the memory usage. Including physical memory (RAM) and swap memory (swap).

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.

1. Use the free command

The free command is the simplest and most commonly used memory viewing command in Linux systems, examples are as follows:

zz@xiaole:~$ free -m
              总计         已用        空闲      共享    缓冲/缓存    可用
内存:       15659        8709         165        1477        6785        5144
交换:       15624        1344       14280
(xonnx) zz@xiaole:~$ free -h
              总计         已用        空闲      共享    缓冲/缓存    可用
内存:        15Gi       8.5Gi       197Mi       1.4Gi       6.6Gi       5.1Gi
交换:        15Gi       1.3Gi        13Gi

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 表示总共有 7822MB 的物理内存(RAM),即7.6G。
used 表示物理内存的使用量,大约是 322M。
free 表示空闲内存;
shared 表示共享内存?;
buff/cache 表示缓存和缓冲内存量; Linux 系统会将很多东西缓存起来以提高性能,这部分内存可以在必要时进行释放,给其他程序使用。
available 表示可用内存;

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

2. 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.

zz@xiaole:~$ cat /proc/meminfo 
MemTotal:       16035748 kB
MemFree:          208956 kB
MemAvailable:    5314984 kB
Buffers:          494680 kB
Cached:          5616432 kB
SwapCached:        19092 kB
Active:          3896780 kB
Inactive:       10010360 kB
Active(anon):     992028 kB
Inactive(anon):  8279308 kB
Active(file):    2904752 kB
Inactive(file):  1731052 kB
Unevictable:      484872 kB
Mlocked:            2492 kB
SwapTotal:      15999996 kB
SwapFree:       14608892 kB
Dirty:             37508 kB
Writeback:             0 kB
AnonPages:       8269488 kB
Mapped:           827992 kB
Shmem:           1478876 kB
KReclaimable:     809792 kB
Slab:            1034028 kB
SReclaimable:     809792 kB
SUnreclaim:       224236 kB
KernelStack:       33056 kB
PageTables:        96408 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    24017868 kB
Committed_AS:   26950740 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      113240 kB
VmallocChunk:          0 kB
Percpu:             8896 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:     1494048 kB
DirectMap2M:    14925824 kB
DirectMap1G:     1048576 kB

Focus on these data:

MemTotal, 总内存
MemFree, 空闲内存
MemAvailable, 可用内存
Buffers, 缓冲
Cached, 缓存
SwapTotal, 交换内存
SwapFree, 空闲交换内存

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

3. Use the vmstat command

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

Examples are as follows:

zz@xiaole:~$ vmstat -s
     16035748 K total memory
      8925540 K used memory
      3833796 K active memory
     10021940 K inactive memory
       235024 K free memory
       495068 K buffer memory
      6380116 K swap cache
     15999996 K total swap
      1398784 K used swap
     14601212 K free swap
      1200660 non-nice user cpu ticks
         6440 nice user cpu ticks
       287555 system cpu ticks
     12629333 idle cpu ticks
      3373867 IO-wait cpu ticks
            0 IRQ cpu ticks
        57036 softirq cpu ticks
            0 stolen cpu ticks
     15916951 pages paged in
     70642617 pages paged out
        11714 pages swapped in
       358647 pages swapped out
    109474456 interrupts
    209512321 CPU context switches
   1687828473 boot time
       101087 forks

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

4. 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:

zz@xiaole:~$ top

top - 15:25:25 up  6:10,  1 user,  load average: 2.73, 2.77, 2.53
任务: 429 total,   1 running, 422 sleeping,   0 stopped,   6 zombie
%Cpu(s):  1.8 us,  1.2 sy,  0.0 ni, 73.0 id, 23.8 wa,  0.0 hi,  0.3 si,  0.0 st
MiB Mem :  15659.9 total,    225.7 free,   8710.2 used,   6724.0 buff/cache
MiB Swap:  15625.0 total,  14259.0 free,   1366.0 used.   5183.1 avail Mem 
Renice PID 9586 to value 
 进程号 USER      PR  NI    VIRT    RES    SHR    %CPU  %MEM     TIME+ COMMAND 

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.

5. htop command

Similar to the top command, but htop also displays various other information, and supports color display.
insert image description hereThe 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

2. View physical memory information

To view the hardware information of physical memory, you can use the demidecode command.
This command outputs information about physical memory.

zz@xiaole:~$ sudo dmidecode -t 17
# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 3.2.0 present.

Handle 0x0002, DMI type 17, 92 bytes
Memory Device
	Array Handle: 0x0001
	Error Information Handle: Not Provided
	Total Width: 64 bits
	Data Width: 64 bits
	Size: 8192 MB
	Form Factor: SODIMM
	Set: None
	Locator: Controller0-ChannelA-DIMM0
	Bank Locator: BANK 0
	Type: DDR4
	Type Detail: Synchronous
	Speed: 3200 MT/s
	Manufacturer: SK Hynix
	Serial Number: 22E2F3F7
	Asset Tag: None
	Part Number: HMAA1GS6CJR6N-XN    
	Rank: 1
	Configured Memory Speed: 3200 MT/s
	Minimum Voltage: Unknown
	Maximum Voltage: Unknown
	Configured Voltage: 1.2 V
	Memory Technology: DRAM
	Memory Operating Mode Capability: Volatile memory
	Firmware Version: Not Specified
	Module Manufacturer ID: Bank 1, Hex 0xAD
	Module Product ID: Unknown
	Memory Subsystem Controller Manufacturer ID: Unknown
	Memory Subsystem Controller Product ID: Unknown
	Non-Volatile Size: None
	Volatile Size: 8 GB
	Cache Size: None
	Logical Size: None

Handle 0x0003, DMI type 17, 92 bytes
Memory Device
	Array Handle: 0x0001
	Error Information Handle: Not Provided
	Total Width: 64 bits
	Data Width: 64 bits
	Size: 8192 MB
	Form Factor: SODIMM
	Set: None
	Locator: Controller1-ChannelA-DIMM0
	Bank Locator: BANK 0
	Type: DDR4
	Type Detail: Synchronous
	Speed: 3200 MT/s
	Manufacturer: SK Hynix
	Serial Number: 22E2F69D
	Asset Tag: None
	Part Number: HMAA1GS6CJR6N-XN    
	Rank: 1
	Configured Memory Speed: 3200 MT/s
	Minimum Voltage: Unknown
	Maximum Voltage: Unknown
	Configured Voltage: 1.2 V
	Memory Technology: DRAM
	Memory Operating Mode Capability: Volatile memory
	Firmware Version: Not Specified
	Module Manufacturer ID: Bank 1, Hex 0xAD
	Module Product ID: Unknown
	Memory Subsystem Controller Manufacturer ID: Unknown
	Memory Subsystem Controller Product ID: Unknown
	Non-Volatile Size: None
	Volatile Size: 8 GB
	Cache Size: None
	Logical Size: None

The information displayed by dmidecode -t 17 includes memory size, type, bandwidth and other information. Of course, because the examples use virtual machines, some information is not available.

If the -t parameter is not added, all types of hardware information will be displayed. You can also specify other -t parameters to view the corresponding hardware information.

summary

All the commands introduced in this article can be executed on the shell terminal and do not need a graphical interface to display.
If you use a Linux desktop system with a graphical interface, using graphical tools is more intuitive. Generally speaking, the graphical desktop will provide corresponding system monitoring tools. Output easy-to-understand cpu, physical memory, swap memory, network bandwidth and other resource usage information.

reference link

https://www.binarytides.com/linux-command-check-memory-usage/
http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html

Guess you like

Origin blog.csdn.net/zyq880625/article/details/131417932