Unlike Windows, Linux often doesn’t have a graphical interface to work with, especially in a server environment.

As a Linux administrator, it is important to know how to obtain the status of currently available and used resources, such as memory, CPU, disks, and so on. If an application is taking up too much resources on your system, and your system is not performing optimally, then you need to find and fix it.

If you want to find the top 10 memory consuming processes, you need to read how to find the most memory consuming processes in Linux.

In Linux, commands do anything, so use the relevant commands. In this tutorial, we will show you eight useful commands to check memory usage, including RAM and swap, on a Linux system.

Creating a swap partition is very important in Linux. If you want to know how to create a swap partition, you can read this article: Creating a swap partition on Linux.

The following commands can help you view Linux memory usage in different ways.

  • freeThe command
  • /proc/meminfofile
  • vmstatThe command
  • ps_memThe command
  • smemThe command
  • topThe command
  • htopThe command
  • glancesThe command

1) How to use the free command to check Linux memory usage

The free command is a major command widely used by Linux administrators. But it provides less information than the /proc/meminfo file.

The free command shows the used and unused amounts of physical and swap memory, as well as the buffers and caches used by the kernel.

This information is obtained from the /proc/meminfo file.

# free -m
              total        used        free      shared  buff/cache   available
Mem:          15867        9199        1702        3315        4965        3039
Swap:         17454         666       16788
Copy the code
  • total: Total memory
  • used: Amount of memory used by the currently running process (used = totalfreebuff/cache)
  • free: Amount of unused memory (free = totalusedbuff/cache)
  • shared: Amount of memory shared between two or more processes
  • buffers: The amount of memory reserved for the kernel to log process queue requests
  • cache: The size of the page buffer that stores recently used files in RAM
  • buff/cache: Total amount of memory used by buffers and caches
  • available: Amount of memory available to start new applications (excluding swap partitions)

2) How to use the /proc/meminfo file to check Linux memory usage

The /proc/meminfo file is a virtual file that contains real-time information about various memory usages. It shows the units of memory state in kilobytes, most of which are hard to understand. However, it also contains useful information about memory usage.

# cat /proc/meminfo

MemTotal:       16248572 kB
MemFree:         1764576 kB
MemAvailable:    3136604 kB
Buffers:          234132 kB
Cached:          4731288 kB
SwapCached:        28516 kB
Active:          9004412 kB
Inactive:        3552416 kB
Active(anon):    8094128 kB
Inactive(anon):  2896064 kB
Active(file):     910284 kB
Inactive(file):   656352 kB
Unevictable:          80 kB
Mlocked:              80 kB
SwapTotal:      17873388 kB
SwapFree:       17191328 kB
Dirty:               252 kB
Writeback:             0 kB
AnonPages:       7566736 kB
Mapped:          3692368 kB
Shmem:           3398784 kB
Slab:             278976 kB
SReclaimable:     125480 kB
SUnreclaim:       153496 kB
KernelStack:       23936 kB
PageTables:        73880 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    25997672 kB
Committed_AS:   24816804 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
Percpu:             3392 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:     1059088 kB
DirectMap2M:    14493696 kB
DirectMap1G:     2097152 kB
Copy the code

3) How to use the vmstat command to check the Linux memory usage

The vmstat command is another useful tool for reporting virtual memory statistics.

Vmstat reports process, memory, page mapping, block I/O, trap, disk, and CPU feature information. Vmstat does not require special permissions, and it can help diagnose system bottlenecks.

# vmstat

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 682060 1769324 234188 4853500  0    3    25    91   31   16 34 13 52  0  0
Copy the code

If you want to see what each means in detail, read the descriptions below.

  • procsProcess:
    • r: Number of processes that can run (running or waiting to run)
    • b: Number of processes in uninterruptible sleep
  • memory: memory
    • swpd: Indicates the amount of virtual memory used
    • free: Amount of free memory
    • buff: The amount of memory used as buffer
    • cache: The amount of cache memory used
    • inact: Amount of inactive memory (usage-aOption)
    • active: Amount of active memory (usage-aOption)
  • Swap: Swap partition
    • si: Amount of memory swapped from disk per second
    • so: Amount of memory exchanged to disk per second
  • IO: Input output
    • bi: Blocks received from a block device (blocks per second)
    • bo: Blocks sent to a block device (blocks per second)
  • System: the system
    • in: Number of interrupts per second, including the clock.
    • cs: Number of context switches per second.
  • CPUThe following are percentages of total CPU time
    • us: Percentage of time spent on non-kernel code (including user time, scheduling time)
    • sy: Percentage of time spent on kernel code (system time)
    • id: Percentage of time spent idle. Prior to Linux 2.5.41, I/O wait times were included
    • wa: Percentage of time spent on I/O waiting. Included in idle time prior to Linux 2.5.41
    • st: Percentage of time stolen by VMS. Prior to Linux 2.6.11, this part was known as Unknown

Run the following command to view the details.

# vmstat -s16248580 K total memory 2210256 K used memory 2311820 K active memory 2153352 K inactive memory 11368812 K free memory 107584 K buffer memory 2561928 K swap cache 17873388 K total swap 0 K used swap 17873388 K free swap 44309 non-nice user  cpu ticks 164 nice user cpu ticks 14332 system cpu ticks 382418 idle cpu ticks 1248 IO-wait cpu ticks 1407 IRQ cpu ticks 2147 softirq cpu ticks 0 stolen cpu ticks 1022437 pages pagedin
          260296 pages paged out
               0 pages swapped in
               0 pages swapped out
         1424838 interrupts
         4979524 CPU context switches
      1577163147 boot time
            3318 forks
Copy the code

4) How to use ps_mem to check Linux memory usage

Ps_mem is a simple Python script for viewing current memory usage. The tool can determine how much memory is used per program (not per process).

The tool calculates the memory used by each program as follows: Total usage = private memory used by the program processes + memory shared by the program processes.

Computing shared memory is a disadvantage, and this tool automatically selects the most accurate method for a running kernel.

# ps_memPrivate + Shared = RAM Used Program 180.0 KiB + 30.0 KiB = 210.0 KiB XF86 - Video-intel-Backlight-helper (2) 192.0 KiB + 66.0 KiB = 258.0 KiB CAT (2) 312.0 KiB + 38.5 KiB = 350.5 KiB lvmetad 380.0 KiB + 25.5 KiB = 405.5 KiB Crond 392.0 KiB + 32.5 KiB = 424.5 KiB rtkit-daemon 852.0 KiB + 117.0 KiB = 969.0kib gnome-session-ctl (2) 928.0kib + 56.5 KiB = 984.5 KiB GVVFS - MTP-volume-monitor 1.0 MiB + 42.5KIb = 1.0 MiB Dconf-service 1.0 MiB + 106.5KIb = 1.1 MiB Gvs-goa-volume-monitor 1.0 MiB + 180.5KIb = 1.2 MiB GVFSD.. 5.3 MiB + 3.0 MiB = 8.3 MiB Evolution-addressbook-factory 8.5 MiB + 1.2 MiB = 9.7 MiB GNOMe-session-binary (4) 7.5 MiB + 3.1 MiB = 10.5 MiB PolKITd 7.4 MiB + 3.3 MiB = 10.7 MiB PulseAudio (2) 7.0 MiB + 7.0 MiB = 14.0 MiB MSM_notifier 12.7 MiB + 2.3 MiB = 15.0 MiB Evolution-source-registry 13.3 MiB + 2.5 MiB = 15.8 MiB GNOMe-terminal-Server 15.8 MiB + 1.0 MiB = 16.8 MiB Tracker-miner-fs 18.7 MiB + 1.8 MiB = 20.5 MiB PYTHon3.7 16.6 MiB + 4.0 MiB = 20.5 MiB Evolution-calendar-factory 22.3 MiB + 753.0 KiB = 23.0 MiB GSD-Keyboard (2) 22.4 MiB + 832.0 KiB = 23.2 MiB GSD-WACOM (2) 20.8 MiB + 2.5 MiB = 23.3 MiB Eman - Tray 22.0 MiB + 1.8 MiB = 23.8 MiB Eman - Applet 23.1 MiB + 934.0 KiB = 24.0 MiB GSD-XSettings (2) 23.7 MiB + 1.2 MiB = 24.9 MiB GSD-media-Keys (2) 23.4 MiB + 1.6 MiB = 25.0 MiB GSD-Color (2) 23.9 MiB + 1.2 MiB = 25.1 MiB Gsd-power (2) 16.5 MiB + 8.9 MiB = 25.4 MiB evolution-alarm-notify 27.2 MiB + 2.0 MiB = 29.2 MiB SystemD-JOURNALD 28.7 MiB + 2.8 MiB = 31.5 MiB C 29.6 MiB + 2.2 MiB = 31.8 MiB Chrome-gnOMe-sh (2) 43.9 MiB + 6.8 MiB = 50.7 MiB WebExtensions 46.7 MiB + 6.7 MiB = 53.5 MiB Goa-Daemon 86.5 MiB + 55.2 MiB = 141.7 MiB Xorg (2) 191.4 MiB + 24.1 MiB = 215.4 MiB Notepadqq-bin 306.7 MiB + 29.0 MiB = 335.7 MiB GNOME-Shell (2) 601.6 MiB + 77.7 MiB = 679.2 MiB Firefox 1.0 GiB + 109.7 MiB = 1.1 GiB chrome (15) 2.3 GiB MiB = 2.5 + 123.1 GiB Web Content (8) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 5.6 GiB = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code

5) How to use smem command to check Linux memory usage

Smem is a tool that provides a variety of memory usage reports for 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) : The amount of libraries and applications used in a virtual memory system.
  • Unique set size (USS) : This reports non-shared memory.
  • Resident set size (RSS) : Physical memory (usually shared by multiple processes) usage, which is usually higher than memory usage.
# smem -tkPID User Command Swap USS PSS RSS 3383 daygeek cat 0 92.0K 123.0K 1.7m 3384 Daygeek cat 0 100.0k 129.0k 1.7m 1177 Daygeek /usr/lib/gnome-session-ctl 0 436.0k 476.0k 4.6m 1171 daygeek /usr/bin/dbus-daemon --conf 0 524.0k 629.0k 3.8m 1238 daygeek /usr/lib/xdg-permission-sto 0 592.0k 681.0k 5.9m 1350 daygeek /usr/lib/gsd-screensaver-pr 0 652.0k 701.0k 5.8m 1135 daygeek /usr/lib/gdm-x-session --ru 0 648.0k 723.0k 6.0m.. 1391 daygeek /usr/lib/evolution-data-ser 0 16.5m 25.2 M, 63.3 M 1416 daygeek caffeine - ng zero 28.7 M, 31.4 M, 66.2 M 4855 daygeek/opt/Google/chrome/chrome - 0, 38.3 M, 46.3 M, 120.6 M 2174 daygeek /usr/lib/firefox/ Firefox -c 0 44.0m 50.7m 120.3m 1254 daygeek /usr/lib/goa-daemon 0 46.7m 53.3m 80.4m 3416 Daygeek/opt/Google/chrome/chrome - 0, 44.7 M, 54.2 M, 103.3 M 4782 daygeek/opt/Google/chrome/chrome - 0, 57.2 M, 65.8 M, 142.3 M 1137 daygeek/usr/lib/Xorg vt2-3376 daygeek displayf 0, 77.2 M, 129.6 M, 192.3 M/opt/Google/chrome/chrome 0 117.8 M to 131.0 M 210.9 M 4448 daygeek/usr/lib/firefox/firefox - c 0, 124.4 M, 133.8 M, 224.1 M 3558 daygeek/opt/Google/chrome/chrome - 0 to 157.3 M 165.7m 242.2m 2310 daygeek /usr/lib/firefox/ Firefox -c 0 159.6m 169.4m 259.6m 4331 daygeek /usr/lib/firefox/firefox -c 0 176.8 M, 186.2 M, 276.8 M 4034 daygeek/opt/Google/chrome/chrome - 0, 179.3 M, 187.9 M, 264.6 M 3911 daygeek / opt/Google/chrome/chrome - 0, 183.1 M, 191.8 M, 269.4 M 3861 daygeek/opt/Google/chrome/chrome - 0, 199.8 M, 208.2 M, 285.2 M 2746 daygeek /usr/bin/.. /lib/notepadq/n 0 193.5m 217.5m 261.5m 1194 daygeek /usr/bin/gnOMe-shell 0 203.7m 219.0m 285.1m 2945 daygeek /usr/lib/firefox/ Firefox -c 0 294.5m 308.0m 410.2m 2786 daygeek /usr/lib/firefox/ Firefox -c 0 341.2m 354.3m 459.1m 4193 Daygeek /usr/lib/firefox/ Firefox -c 0 417.4m 427.6m 519.3m 2651 daygeek /usr/lib/firefox/firefox -c 0 417.0m 430.1m 535.6m 2114 daygeek /usr/lib/firefox/ Firefox -c 0 430.6m 473.9m 610.9m 2039 daygeek /usr/lib/firefox/firefox -- 0 601.3m 677.5 M, 840.6 M -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 90 1 0 4.8 G 5.2 G 8.0 GCopy the code

6) How to use the top command to check Linux memory usage

The top command is most commonly used by Linux administrators to view the resource usage of a process.

This command shows the total amount of memory on the system, the current amount of memory used, the amount of free memory, and the total amount of memory used by the buffer. In addition, the command shows the total amount of swap space, the amount of memory currently used, the amount of free swap space, and the total amount of memory used by the cache.

# top -b | head -10Top-11:04:39 UP 40 min, 1 user, Load Average: 1.59, 1.42, 1.28 Tasks: 288 total, 2 running, 286 sleeping, 0 stopped, 0 zombie %Cpu(s): 13.3US, 1.5sy, 0.0Ni, 84.4ID, 0.0wa, 0.3Hi, 0.5Si, 0.0st KiB Mem: 16248572 total, 7755928 free, 4657736 used, 3834908 buff/cache KiB Swap: 17873388 total, 17873388 free, 0 used. 9179772 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2114 daygeek 20 3182736 616624 328228 R 83.3 3.8 7:09.72 Web Content 2039 DayGeek 20 4437952 849616 261364 S 13.3 5.2 7:58.54 Firefox 1194 DayGeek 20 4046856 291288 165460 S 4.2 1.8 1:57.68 GnOMe-Shell 4034 Daygeek 20 808556 273244 88676 S 4.2 1.7 1:44.72 Chrome 2945 DayGeek 20 3309832 416572 150112s 3.3 2.6 4:04.60 Web Content 1137 Daygeek 20 564316 197292 183380s 2.5 1.2 2:55.76 Xorg 2651 Daygeek 20 3098420 547260 275700 S 1.7 3.4 2:15.96 Web Content 2786 Daygeek 20 2957112 463912 240744s 1.7 2.9 3:22.29 Web Content 1 root 20 182756 10208 7760 S 0.8 0.1 0:04.51 systemd 442 root-51 S 0.8 0:05.02 IRq / 141-iW + 1426 daygeek 20 373660 48948 29820 S 0.8 0.3 0:03.55 PYTHon3 2174 daygeek 20 2466680 122196 78604 S 0.8 0.8 0:17.75 WebExtensi+Copy the code

7) How to use the htop command to check Linux memory usage

The htop command is an interactive Linux/Unix system process viewer. It is a text-mode application, and using it requires the Ncurses library developed by Hisham.

This command is designed to be used instead of the top command. This command is similar to the top command, but allows you to scroll vertically or horizontally to see all the processes on the system.

The htop command has different colors, which is an added advantage when you’re tracking system performance.

In addition, you are free to perform process-related tasks, such as killing a process or changing its priority without requiring its process number (PID).

8) How to use glances to check Linux memory usage

Glances is a cross-platform system monitoring tool written in Python.

You can view all information in one place, such as: CPU usage, memory usage, running processes, network interfaces, disk I/O, RAID, sensors, file system information, Docker, system information, running time, etc.


Via: www.2daygeek.com/linux-comma…

Magesh Maruthamuthu (lujun9972

This article is originally compiled by LCTT and released in Linux China