3 Methods to Get Memory Usage on Linux

3 Methods to Get Memory Usage on Linux

Understanding the memory usage of Linux system is essential for resource management and optimal performance. Whether you are a system administrator, a developer, or just a curious user, knowing how to monitor memory usage can help you identify potential bottlenecks and prevent system slowdowns. This tutorial provides 3 methods how to get memory usage on Linux.

Method 1 - free command

The free command is a simple and widely used tool to view the memory usage on a Linux system. It provides detailed information about the total, used, free, and available memory, both in physical and swap memory.

free -h

The -h option will display the output in a human-readable format, showing memory sizes in gigabytes, megabytes, etc.

Output example:

               total        used        free      shared  buff/cache   available
Mem:            15Gi       2.3Gi        10Gi        54Mi       2.8Gi        12Gi
Swap:          4.0Gi          0B       4.0Gi

Method 2 - top command

The top command is an interactive process viewer that also displays memory usage.

top

By default, it shows a real-time view of active processes, along with their memory usage. It displays information about the system’s total, free, and used physical and swap memory.

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

Output example:

top - 04:31:28 up 22 min,  1 user,  load average: 0.22, 0.18, 0.13
Tasks: 396 total,   1 running, 395 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.1 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  15688.9 total,   9352.5 free,   2963.7 used,   3372.8 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.  12308.3 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1056 root      20   0 2095628  42296  30636 S   1.0   0.3   0:08.66 containerd
   1146 mysql     20   0 9891.2m   1.0g  39076 S   1.0   6.8   0:21.32 mysqld
   1041 rabbitmq  20   0 4234372 163580  71536 S   0.7   1.0   0:09.54 beam.smp
    628 systemd+  20   0   14888   6260   5468 S   0.3   0.0   0:01.75 systemd-oomd
   1043 redis     20   0   75636  10920   8292 S   0.3   0.1   0:02.43 redis-server

Method 3 - /proc/meminfo file

On Linux, we can access various system information through the /proc virtual filesystem. The /proc/meminfo file contains detailed information about memory usage and other memory-related statistics. Use cat or similar command to read its contents:

cat /proc/meminfo

The output will display the information about the system's memory and swap usage.

Output example:

MemTotal:       16065460 kB
MemFree:         9673964 kB
MemAvailable:   12702492 kB
Buffers:          122620 kB
Cached:          3168368 kB
SwapCached:            0 kB
Active:           992676 kB
Inactive:        4601720 kB
Active(anon):       2732 kB
Inactive(anon):  2388908 kB
Active(file):     989944 kB
Inactive(file):  2212812 kB
Unevictable:       18188 kB
Mlocked:              16 kB
SwapTotal:       4194300 kB
SwapFree:        4194300 kB
...

Leave a Comment

Cancel reply

Your email address will not be published.