Monitoring memory usage in Linux is essential for maintaining system performance and ensuring efficient resource management. Whether managing a server or using a desktop, knowing how much RAM is being used helps diagnose performance issues and optimize the system. Linux offers several tools to display memory information, enabling users to get a quick overview or detailed statistics.

Many users prefer command-line tools due to their efficiency and simplicity, especially when working on servers or remote connections. Tools like free and top allow users to retrieve memory data directly from the terminal, making them highly suitable for automated scripts or real-time monitoring. These tools display memory usage details for the entire system, including how much memory is allocated to running processes, buffers, and swap space.

In addition to command-line options, graphical tools such as the GNOME System Monitor or KDE Resource Monitor provide a more visual way to view memory consumption. These are typically used on desktops and offer a more user-friendly interface for users less familiar with terminal commands. However, in most server environments, command-line tools remain the standard approach.

Steps to monitor memory activity in Linux:

  1. Open the terminal on your Linux system.
  2. Display overall memory usage using the free command.
    $ free
                  total        used        free      shared  buff/cache   available
    Mem:        2013428      999352      113610        6188      900376      842184
    Swap:        969960         524      969436

    This command shows total, used, free, and available memory along with swap details.

  3. View memory usage in a human-readable format.
    $ free -h
                  total        used        free      shared  buff/cache   available
    Mem:          1.9Gi       976Mi       111Mi       6.0Mi       879Mi       822Mi
    Swap:         947Mi       0.0Ki       946Mi

    The -h option displays memory data in gigabytes, megabytes, and other human-readable formats.

  4. Monitor real-time memory usage using the top command.
    $ top
    top - 16:25:49 up 40 min,  2 users,  load average: 0.08, 0.23, 0.34
    Tasks: 281 total,   1 running, 280 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  5.9 us, 17.6 sy,  0.0 ni, 76.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    MiB Mem :   1966.2 total,    108.4 free,    975.9 used,    881.9 buff/cache

    Press q to exit the top interface.

  5. Install and run htop for detailed memory statistics.
    $ sudo apt install htop   # Ubuntu  
    $ sudo yum install htop   # CentOS
    $ htop

    Use arrow keys to navigate and press F10 to exit htop.

  6. Check memory information from the proc file system.
    $ cat /proc/meminfo
    MemTotal:       15712000 kB
    MemFree:        10035200 kB
    MemAvailable:   12567000 kB

    This provides detailed memory statistics from the kernel.

  7. View memory activity using the vmstat command.
    $ vmstat -s
         15712000 K total memory
          2350500 K used memory
          9847200 K active memory

    vmstat shows a snapshot of memory usage and system activity.

  8. Sort running processes by memory usage using the ps command.
    $ ps aux --sort=-%mem | head
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root      1234  5.1 10.5 123456 23456 ?       Ssl  10:00   2:30 process1

    This command displays the top memory-consuming processes.

Discuss the article:

Comment anonymously. Login not required.