Check memory usage in Linux

Reading time: 10 minutes

Checking memory is very important as it determines the smooth running of a machine. Memory is getting cheaper but each machine has a limited amount and the clock speed is improving but the difference between clock speed and memory access is increasing day by day. To keep our machines healthy, we want to check the memory usage of several processes and in Linux, we have five tools to do so.

Five tools to check memory usage on Linux are:

  • free
  • /proc/meminfo
  • vmstat
  • top
  • dmidecode

free

Use the free command to see the total memory available along with used, free shared, cache and available memory. The memory of swap space is also provided.


opengenus@opengenus:~$ free
              total        used        free      shared  buff/cache   available
Mem:      131897324      528144   113638088       34772    17731092   129785060
Swap:       1000444        7680      992764

/proc/meminfo

See the contents of the meminfo file to get a detailed report of memory usage of current system state.


opengenus@opengenus:~$ cat /proc/meminfo
MemTotal:       131897324 kB
MemFree:        113638212 kB
MemAvailable:   129785184 kB
Buffers:         2558480 kB
Cached:         10392176 kB
SwapCached:          464 kB
Active:          8188260 kB
Inactive:        4977332 kB
Active(anon):      94576 kB
Inactive(anon):   155152 kB
Active(file):    8093684 kB
Inactive(file):  4822180 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       1000444 kB
SwapFree:         992764 kB
Dirty:                 8 kB
Writeback:             0 kB
AnonPages:        215008 kB
Mapped:           203184 kB
Shmem:             34772 kB
Slab:            4780444 kB
SReclaimable:    4480552 kB
SUnreclaim:       299892 kB
KernelStack:       13536 kB
PageTables:        15484 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    66949104 kB
Committed_AS:    2856860 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      576060 kB
DirectMap2M:    26578944 kB
DirectMap1G:    106954752 kB

vmstat

vmstat reports information about processes, memory, paging, block IO, traps, disks and cpu activity.


opengenus@opengenus:~$ vmstat -s
    131897328 K total memory
       528132 K used memory
      8188236 K active memory
      4977328 K inactive memory
    113638088 K free memory
      2558484 K buffer memory
     15172620 K swap cache
      1000444 K total swap
         7680 K used swap
       992764 K free swap
     46122982 non-nice user cpu ticks
         3683 nice user cpu ticks
      6042580 system cpu ticks
   3846368119 idle cpu ticks
        19631 IO-wait cpu ticks
            0 IRQ cpu ticks
         7071 softirq cpu ticks
            0 stolen cpu ticks
      6007292 pages paged in
     97835873 pages paged out
          148 pages swapped in
          426 pages swapped out
   3975461049 interrupts
   4145207951 CPU context switches
   1548368694 boot time
       439221 forks

top

The top command lists all processes based on various parameters like memory usage and cpu usage.

top command output

dmidecode

dmidecode is a tool for displaying the computer's DMI table contents. This table contains a description of the system's hardware components and other information like serial numbers and BIOS revision.


opengenus@opengenus:~$ sudo dmidecode -t 17
# dmidecode 3.0
Getting SMBIOS data from sysfs.
SMBIOS 3.0 present.

Handle 0x002F, DMI type 17, 40 bytes
Memory Device
        Array Handle: 0x0028
        Error Information Handle: 0x002E
        Total Width: 64 bits
        Data Width: 64 bits
        Size: 16384 MB
        Form Factor: DIMM
        Set: None
        Locator: DIMM 0
        Bank Locator: P0 CHANNEL A
        Type: DDR4
        Type Detail: Synchronous Unbuffered (Unregistered)
        Speed: 2133 MHz
        Manufacturer: Unknown
        Serial Number: 00000000
        Asset Tag: Not Specified
        Part Number: CMK64GX4M4B3333C16
        Rank: 2
        Configured Clock Speed: 1067 MHz
        Minimum Voltage: 1.2 V
        Maximum Voltage: 1.2 V
        Configured Voltage: 1.2 V