How to Check Memory in Linux (Simple Commands You Should Know)

What Does “Memory” Mean in Linux?

In Linux, memory usually refers to RAM (Random Access Memory) the temporary working space your system uses to store data and run applications.

When memory fills up, Linux intelligently uses swap space (part of your hard drive) to offload inactive data.

So, checking your memory helps you:

  • Troubleshoot system slowdowns
  • Identify memory-hungry applications
  • Ensure servers or VMs have enough resources
  • Monitor performance for optimization

1. Check Memory Using free Command (Most Common)

The simplest and most widely used command is free.

Command:

free -h

Example Output:

              total        used        free      shared  buff/cache   available
Mem:           15Gi        3.1Gi       8.8Gi       456Mi       3.2Gi        11Gi
Swap:         2.0Gi          0B        2.0Gi

Explanation:

  • total → Total installed RAM
  • used → Memory currently in use
  • free → Completely unused RAM
  • buff/cache → Memory used for caching and buffers (can be reused by apps)
  • available → Actual memory available for new processes

The -h flag makes it “human-readable” (shows GB/MB instead of raw bytes).

Why this is useful:
It’s quick, reliable, and available on every Linux distribution from Ubuntu to CentOS.


2. Check Memory with /proc/meminfo

For a more detailed view, Linux stores real-time memory stats in the /proc/meminfo file.

Command:

cat /proc/meminfo

Example Output (shortened):

MemTotal:       16329600 kB
MemFree:         9012340 kB
MemAvailable:   11845672 kB
Buffers:          512000 kB
Cached:          2123456 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB

Explanation:

  • MemTotal → Total physical memory
  • MemFree → Currently unused memory
  • MemAvailable → Estimated available memory for new apps
  • Cached → Memory used by file cache
  • SwapTotal/SwapFree → Swap space information

This file is updated dynamically, so you’re always seeing live data.


3. Check Memory with vmstat Command

vmstat (Virtual Memory Statistics) gives a more technical overview of memory, process, and system activity.

Command:

vmstat -s

Example Output:

     16329600 K total memory
      3101524 K used memory
      8812340 K free memory
      3212340 K buffer memory
     2097148 K swap total
           0 K swap used

Useful for:
Monitoring performance over time, analyzing memory swapping behavior, and diagnosing memory leaks.

You can also use:

vmstat 2 5

This updates memory stats every 2 seconds for 5 cycles — great for watching live memory activity.


4. Use top Command (Real-Time Memory Monitoring)

The top command shows memory usage for every running process.

Command:

top

Example Output (top section):

MiB Mem :  15946.0 total,   3156.5 used,   8842.9 free,    3234.2 buff/cache
MiB Swap:   2048.0 total,      0.0 used,   2048.0 free.   11231.0 avail Mem

Below this section, you’ll see a live list of processes sorted by CPU or memory usage.

Shortcuts inside top:

  • Press M → Sort by memory usage
  • Press q → Quit

Why it’s useful:
You can see which processes are consuming the most RAM in real time.


5. Use htop for a More Visual Display

htop is an enhanced version of top with a color-coded interface and easy navigation.

Install (if not already installed):

sudo apt install htop   # Ubuntu/Debian
sudo dnf install htop   # Fedora
sudo yum install htop   # CentOS/RHEL

Run:

htop

What You’ll See:

  • Total, used, and free memory (and swap)
  • CPU usage bars
  • List of processes with color indicators
  • Interactive controls (use arrow keys and F10 to quit)

Why it’s better:
You get a real-time graphical overview — perfect for diagnosing performance issues at a glance.


6. Check Memory Per Process Using ps

To see which processes are using the most memory, use:

ps aux --sort=-%mem | head

Example Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1234  3.4 12.5 2500000 205000 ?      Sl   10:25   0:10 /usr/lib/firefox/firefox
mysql     2048  2.0 10.1 3400000 165000 ?      Sl   10:26   0:05 /usr/sbin/mysqld

Explanation:

  • %MEM → Percentage of RAM used
  • VSZ → Virtual memory size
  • RSS → Resident Set Size (actual physical memory used)

This helps you identify memory-heavy processes for optimization or debugging.


7. Check Swap Memory Usage

Swap acts as backup memory when RAM is full.

Command:

swapon --show

Example Output:

NAME      TYPE  SIZE  USED PRIO
/swapfile file  2G    0B   -2

To see swap summary:

free -h

Tip:
If your system is using too much swap even with enough RAM, it might indicate memory leaks or misconfigured applications.


8. Use sar for Historical Memory Data

If you have the sysstat package installed, you can view memory usage trends over time.

Install:

sudo apt install sysstat

Command:

sar -r 1 3

This displays memory statistics every second for 3 intervals.

Why it’s useful:
Great for performance analysis or diagnosing memory-related slowdowns that occurred in the past.


9. Use dmidecode to Check Physical Memory Hardware

If you want to know your RAM type, size, and slots, use:

sudo dmidecode --type memory

Example output (simplified):

Memory Device
        Size: 8192 MB
        Type: DDR4
        Speed: 2666 MT/s

When to use:
When upgrading RAM or checking hardware details on physical machines.


Summary of Commands

CommandPurpose
free -hQuick overview of total and available memory
cat /proc/meminfoDetailed memory statistics
vmstatVirtual memory usage and system performance
topReal-time process and memory usage
htopInteractive, colorful view of system memory
ps aux --sort=-%memFind processes consuming the most RAM
swapon --showView swap memory usage
dmidecode --type memoryCheck hardware-level memory details

Example Use Case: Checking Memory Before Running Heavy Tasks

Let’s say you’re about to compile a large program or train a machine learning model.

You can quickly check available memory:

free -h

Output:

Mem: 15Gi total, 3.1Gi used, 11Gi available

Now you know there’s enough RAM to safely run your program without crashing the system or using too much swap.


Memory Management Tips for Linux Users

  • Close unused applications regularly.
  • Monitor background services (using htop).
  • Add more swap space if you run out of RAM frequently.
  • Keep your system updated — newer kernels optimize memory handling.
  • For servers, use monitoring tools like Glances, Netdata, or Prometheus for automated alerts.

Conclusion

Checking memory usage in Linux is one of the most basic yet important system monitoring tasks.

The fastest and most reliable method is:

free -h

But depending on what you need real-time monitoring, detailed reports, or per-process usage you can use tools like top, htop, vmstat, or ps.

Learning to read memory usage properly helps you troubleshoot performance issues, optimize resource allocation, and keep your Linux system running smoothly.


Discover more from PratsDigital

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *