15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
15.11.2023

Commands and Tools to Check RAM Consumption in Linux

Monitoring RAM usage in Linux means querying the kernel's memory subsystem to retrieve metrics on physical memory allocation, swap utilization, and per-process resident set sizes. The most direct methods use built-in utilities — free, top, htop, ps, vmstat, and smem — each exposing a different layer of the memory hierarchy, from system-wide totals down to per-process proportional set size (PSS).

Excessive memory pressure triggers the Linux Out-of-Memory (OOM) killer, which forcibly terminates processes to reclaim RAM. Understanding which commands reveal which metrics — and what those metrics actually mean — is the difference between reactive firefighting and proactive capacity management. This guide covers every major tool, the kernel data sources they read from, and the edge cases that catch even experienced administrators off guard.

Why RAM Monitoring Matters on Linux Servers

Linux memory management is deliberately aggressive. The kernel uses all available RAM as a page cache to accelerate disk I/O, which means a system reporting near-zero free memory is not necessarily under pressure — it may simply be caching efficiently. Misreading this behavior is one of the most common mistakes when interpreting raw memory figures.

Key reasons to monitor RAM continuously:

  • OOM killer prevention: Identify memory-hungry processes before the kernel terminates critical services.
  • Swap usage detection: Heavy swap activity (swapping) indicates RAM exhaustion and causes severe I/O latency.
  • Memory leak diagnosis: Processes with steadily growing RSS over time signal application-level leaks.
  • Capacity planning: Trend data informs decisions about vertical scaling or workload redistribution.
  • Performance tuning: Adjusting vm.swappiness, huge pages, and NUMA topology requires baseline memory data.

On a VPS Hosting environment where resources are shared or capped by hypervisor limits, accurate RAM monitoring is especially critical — hitting a memory ceiling silently degrades performance before any alert fires.

Understanding Linux Memory Terminology

Before running any command, you need to understand what the output columns actually represent.

TermDefinition
TotalPhysical RAM installed (or allocated to the VM)
UsedMemory actively consumed by processes and kernel structures
FreeCompletely unused RAM — often misleadingly low
SharedMemory used by tmpfs and shared between processes
Buff/CacheKernel buffers and page cache — reclaimable on demand
AvailableRealistic estimate of RAM available for new allocations without swapping
Swap UsedData evicted from RAM to the swap partition or swap file
VSZ (Virtual Size)Total virtual address space reserved by a process
RSS (Resident Set Size)Physical RAM currently occupied by a process
PSS (Proportional Set Size)RSS adjusted for shared memory — the most accurate per-process metric
USS (Unique Set Size)Memory exclusively owned by a process; freed entirely on exit

Critical insight: Always use the Available column from free -h, not the Free column, when assessing whether a system has enough RAM for a new workload. The Free column ignores reclaimable cache, leading to false alarms.

The /proc/meminfo File: The Authoritative Source

Every tool described in this article reads from /proc/meminfo, a virtual file maintained by the kernel in real time. Inspecting it directly gives you the most granular data available:

cat /proc/meminfo

Key fields to watch:

  • MemTotal — total usable RAM
  • MemFree — completely idle RAM
  • MemAvailable — estimated available RAM for new processes
  • Buffers — raw disk block cache
  • Cached — page cache for files
  • SwapTotal / SwapFree — swap capacity and availability
  • Dirty — memory waiting to be written to disk (high values indicate I/O pressure)
  • HugePages_Total / HugePages_Free — huge page allocation status
  • Slab — kernel data structure cache (can grow large on busy NFS or database servers)

Understanding /proc/meminfo lets you interpret every other tool's output with full context.

Checking RAM with the free Command

free is the fastest way to get a system-wide memory snapshot. It reads directly from /proc/meminfo and formats the output into a human-readable table.

Basic Usage

free

Output is in kilobytes by default. Use flags for more readable formats:

free -h        # Human-readable (MB/GB)
free -m        # Output in megabytes
free -g        # Output in gigabytes
free -s 5      # Refresh every 5 seconds (continuous monitoring)
free -h --si   # Use SI units (1000-based) instead of binary (1024-based)

Sample Output Explained

              total        used        free      shared  buff/cache   available
Mem:           15Gi       4.2Gi       1.1Gi       312Mi       9.8Gi       10.9Gi
Swap:         2.0Gi       128Mi       1.9Gi

In this example, the system appears to have only 1.1 GB free, but 10.9 GB is actually available for new processes because the kernel will reclaim buff/cache on demand. This is the single most important thing to understand about free output.

Edge Case: Swap Usage as a Warning Signal

Even a small amount of swap usage (Swap: used > 0) on a production server warrants investigation. It means the kernel has already decided some memory pages are better stored on disk than in RAM — a sign that the working set exceeds physical memory.

Checking RAM with the top Command

top provides a continuously updated, real-time view of system resource usage, combining CPU and memory statistics with a ranked process list.

top

Key Memory Fields in top

At the top of the top interface, two lines show memory statistics:

MiB Mem :  16384.0 total,   1126.4 free,   4301.2 used,  10956.4 buff/cache
MiB Swap:   2048.0 total,   1920.0 free,    128.0 used.  11200.0 avail Mem

In the process table, the most relevant memory columns are:

  • VIRT — virtual memory size (VSZ equivalent)
  • RES — resident physical memory (RSS)
  • SHR — shared memory portion of RES
  • %MEM — percentage of total physical RAM used

Useful top Interactive Commands

KeyAction
MSort processes by memory usage (%MEM)
PSort by CPU usage
kKill a process by PID
1Toggle per-CPU core statistics
fAdd/remove display fields
WSave current configuration

Running top Non-Interactively

For scripting and log capture, run top in batch mode:

top -b -n 1 | head -30

This outputs a single snapshot — useful for cron-based monitoring scripts.

Checking RAM with htop

htop is an enhanced, ncurses-based process viewer that presents the same underlying data as top with a significantly more usable interface. It is not installed by default on most distributions.

Installation

# Debian / Ubuntu
apt-get install htop

# RHEL / CentOS / AlmaLinux / Rocky Linux
yum install htop
# or on newer versions:
dnf install htop

# Alpine Linux
apk add htop

What htop Adds Over top

  • Color-coded memory bars distinguishing used memory, buffers, and cache at a glance
  • Horizontal and vertical process tree view (F5)
  • Mouse support for clicking and scrolling
  • Multi-select for bulk process management
  • Built-in search (F3) and filter (F4) without memorizing key bindings
  • Direct display of available memory prominently in the header

htop Memory Bar Color Legend

ColorMeaning
GreenUsed memory (processes)
BlueBuffer cache
Yellow/OrangePage cache
RedSwap used

Pro tip: In htop, press F2 (Setup) > Display Options > enable "Show CPU frequency" and "Detailed CPU time" for a more complete picture alongside memory data.

Checking RAM with the ps Command

ps (process status) queries the kernel's process table and can be sorted and filtered to identify the top memory consumers at a point in time.

Sort Processes by Memory Consumption

ps aux --sort=-%mem

Output Columns Explained

ColumnDescription
USEROwner of the process
PIDProcess ID
%CPUCPU utilization since process start
%MEMPercentage of physical RAM used (RSS / MemTotal)
VSZVirtual memory size in KB
RSSResident set size in KB
TTYControlling terminal
STATProcess state (S=sleeping, R=running, Z=zombie, D=uninterruptible wait)
STARTProcess start time
TIMECumulative CPU time consumed
COMMANDCommand name and arguments

Practical One-Liners

Show the top 10 memory-consuming processes with human-readable output:

ps aux --sort=-%mem | head -11

Show memory usage for a specific process name (e.g., Apache):

ps aux | grep apache2 | awk '{sum += $6} END {print sum/1024 " MB"}'

This sums the RSS values of all apache2 worker processes — critical for understanding the true footprint of multi-process web servers.

Important caveat: ps shows RSS, which double-counts shared libraries. If 20 Apache workers each map the same 50 MB shared library, ps reports 1,000 MB of shared library usage across them, while the actual physical cost is just 50 MB. Use smem for accurate accounting.

Checking RAM with smem

smem is the most accurate per-process memory accounting tool available on Linux because it reports PSS (Proportional Set Size) — shared memory is divided proportionally among all processes that map it, eliminating the double-counting problem inherent in RSS-based tools.

Installation

# Ubuntu / Debian
apt-get install smem

# CentOS / RHEL 7
yum install smem

# RHEL 8+ / AlmaLinux / Rocky Linux
dnf install smem

# Alternatively, install via pip
pip install smem

Basic Usage

smem

Useful smem Options

smem -r              # Sort by RSS (descending)
smem -s pss -r       # Sort by PSS (most accurate, descending)
smem -u              # Aggregate by user
smem -m              # Show system-wide memory map
smem -p              # Show percentages instead of raw KB
smem -k              # Show values in KB
smem -t              # Show totals row
smem --pie name      # Generate a pie chart (requires matplotlib)
smem -P apache2      # Filter by process name pattern

PSS vs RSS: Why It Matters

Consider a server running 10 Node.js worker processes, each sharing a 100 MB V8 runtime library:

  • RSS per process: 200 MB (100 MB shared + 100 MB private)
  • Total RSS reported: 2,000 MB
  • PSS per process: 110 MB (10 MB share of 100 MB + 100 MB private)
  • Total PSS reported: 1,100 MB
  • Actual physical RAM used: 1,100 MB

RSS overstates usage by nearly 2x in this scenario. When making scaling decisions on a Dedicated Server, using PSS from smem gives you the ground truth.

Checking RAM with vmstat

vmstat (virtual memory statistics) provides a broader view of memory, swap, I/O, and CPU activity, making it ideal for diagnosing system-wide memory pressure rather than individual process issues.

vmstat 2 10    # Report every 2 seconds, 10 times

Key Memory Columns

ColumnDescription
swpdVirtual memory used (swap)
freeIdle memory
buffMemory used as buffers
cacheMemory used as cache
siMemory swapped in from disk (KB/s)
soMemory swapped out to disk (KB/s)

Critical signal: Non-zero si (swap-in) and so (swap-out) values in a running vmstat stream indicate active swapping — a definitive sign of memory exhaustion. Even occasional spikes in so can cause application latency spikes of hundreds of milliseconds.

Checking RAM with sar (System Activity Reporter)

sar from the sysstat package records historical memory data, enabling retrospective analysis — something real-time tools cannot provide.

Installation

apt-get install sysstat    # Debian/Ubuntu
yum install sysstat        # CentOS/RHEL

Usage

sar -r 1 5          # Memory stats every 1 second, 5 times
sar -r -f /var/log/sysstat/sa$(date +%d)   # Today's historical data
sar -S 1 5          # Swap statistics

sar is invaluable for answering questions like "Was there a memory spike at 3 AM that caused the application crash?" — a question no real-time tool can answer after the fact.

Checking RAM with /proc/<PID>/status and /proc/<PID>/smaps

For deep per-process analysis, the kernel exposes detailed memory maps directly in /proc.

Quick Per-Process Memory Check

cat /proc/$(pgrep nginx | head -1)/status | grep -E "VmRSS|VmPeak|VmSize|VmSwap"

Output example:

VmPeak:   512340 kB
VmSize:   498120 kB
VmRSS:    102400 kB
VmSwap:        0 kB
  • VmPeak — peak virtual memory size ever reached by this process
  • VmRSS — current resident set size
  • VmSwap — how much of this process has been swapped out

Detailed Memory Map with smaps

cat /proc/$(pgrep mysql | head -1)/smaps | grep -E "^(Size|Rss|Pss|Shared|Private)"

This reveals every memory mapping (heap, stack, shared libraries, anonymous mappings) with individual RSS and PSS values — the most granular memory view available without a profiler.

Tool Comparison: Choosing the Right Command

ToolScopeMetric TypeReal-TimeHistoricalShared Mem AccuracyBest Use Case
freeSystem-wideTotal/AvailableYesNoN/AQuick health check
topPer-process + systemRSS, %MEMYesNoLow (RSS)Interactive monitoring
htopPer-process + systemRSS, %MEMYesNoLow (RSS)Interactive monitoring (preferred)
psPer-processRSS, VSZSnapshotNoLow (RSS)Scripting, sorting
smemPer-processPSS, USS, RSSSnapshotNoHigh (PSS)Accurate memory accounting
vmstatSystem-wideSwap I/O, freeYesNoN/ADiagnosing swap pressure
sarSystem-wideAll metricsNoYesN/APost-incident analysis
/proc/meminfoSystem-wideRaw kernel dataYesNoN/AScripting, automation
/proc/PID/smapsPer-processFull mapYesNoHighDeep process profiling

Practical Monitoring Workflows

Workflow 1: Rapid Triage (Under 60 Seconds)

free -h                          # Step 1: Is Available memory critically low?
vmstat 1 5                       # Step 2: Is active swapping occurring?
ps aux --sort=-%mem | head -15   # Step 3: Which processes are the top consumers?

Workflow 2: Identifying a Memory Leak

# Watch a specific process's RSS grow over time
watch -n 5 'ps -o pid,rss,vsz,comm -p $(pgrep your_app)'

# Or use smem with repeated snapshots
while true; do smem -P your_app -t; sleep 30; done

Workflow 3: Post-Incident Historical Analysis

sar -r -f /var/log/sysstat/sa$(date +%d --date='yesterday')

Workflow 4: Automated Alerting Script

#!/bin/bash
THRESHOLD=90
USED_PCT=$(free | awk '/^Mem:/ {printf "%.0f", $3/$2 * 100}')
if [ "$USED_PCT" -gt "$THRESHOLD" ]; then
    echo "ALERT: RAM usage at ${USED_PCT}% on $(hostname)" | mail -s "Memory Alert" admin@example.com
fi

This script can be placed in a cron job for continuous automated monitoring — a practical necessity on any production VPS with cPanel or bare-metal server.

Advanced: Kernel Memory Tuning Parameters

Once you have identified memory pressure, these sysctl parameters directly affect behavior:

# View current swappiness (default: 60)
sysctl vm.swappiness

# Reduce swap tendency (recommended for databases: 10)
sysctl -w vm.swappiness=10

# Increase dirty page write-back aggressiveness
sysctl -w vm.dirty_ratio=15
sysctl -w vm.dirty_background_ratio=5

# Drop page cache manually (use with caution on production)
sync; echo 3 > /proc/sys/vm/drop_caches

vm.swappiness explained: A value of 60 means the kernel will start swapping when RAM is 40% utilized. For database servers (MySQL, PostgreSQL, Redis), setting this to 10 keeps data in RAM far longer, dramatically reducing I/O latency. For desktop systems or systems with limited RAM, the default is more appropriate.

Common Pitfalls and Misinterpretations

Pitfall 1: Panicking over low "free" memory

As explained earlier, Linux intentionally uses free RAM as cache. A system with 200 MB free but 8 GB available is healthy. Always read the Available column.

Pitfall 2: Summing RSS to estimate total memory usage

Summing ps RSS values across all processes will typically exceed total physical RAM due to shared library double-counting. Use smem -t for an accurate system total.

Pitfall 3: Ignoring the Slab cache

On busy NFS clients or servers with many small files, the kernel Slab allocator can consume gigabytes of RAM. Check cat /proc/meminfo | grep Slab — this memory is technically reclaimable but does not appear in process-level tools.

Pitfall 4: Confusing VSZ with actual memory usage

A Java application may show 4 GB VSZ but only 512 MB RSS. VSZ includes memory-mapped files, reserved but unallocated heap, and shared libraries — most of which are never actually loaded into physical RAM.

Pitfall 5: Overlooking memory on containerized workloads

Inside a Docker container, free shows the host's total memory, not the container's cgroup limit. Use cat /sys/fs/cgroup/memory/memory.usage_in_bytes and cat /sys/fs/cgroup/memory/memory.limit_in_bytes for accurate container-level metrics.

Setting Up Persistent RAM Monitoring

For production environments, point-in-time commands are insufficient. Consider these approaches for continuous visibility:

  • sysstat with cron: Enables sar historical data collection automatically.
  • Prometheus + Node Exporter: Scrapes /proc/meminfo metrics and exposes them for Grafana dashboards.
  • Netdata: Zero-configuration real-time monitoring with per-second granularity and built-in memory anomaly detection.
  • Zabbix or Nagios: Enterprise-grade alerting with configurable memory thresholds and escalation policies.

When running workloads on GPU Hosting infrastructure, also monitor GPU VRAM alongside system RAM — nvidia-smi --query-gpu=memory.used,memory.free --format=csv provides the equivalent metrics for GPU memory.

For web hosting environments managed through VPS Control Panels, most panels include built-in memory graphs — but these typically poll at 1–5 minute intervals and will miss short-duration spikes that vmstat or sar would capture.

Technical Decision Matrix: Which Tool to Use When

ScenarioRecommended ToolCommand
Quick system health checkfreefree -h
Find top memory consumers right nowps or htop`ps aux –sort=-%memhead -15`
Accurate per-process accountingsmemsmem -s pss -r
Diagnose active swappingvmstatvmstat 1 10
Investigate a past memory incidentsarsar -r -f /var/log/sysstat/saDD
Profile a specific process deeply/proc/PID/smapscat /proc/PID/smaps
Continuous production monitoringPrometheus + Node Exporter
Container memory limitscgroup filesystemcat /sys/fs/cgroup/memory/memory.usage_in_bytes

Key Takeaways Checklist

  • Always check Available memory from free -h, not the Free column, to assess actual headroom.
  • Use vmstat 1 5 to confirm or rule out active swapping before escalating an investigation.
  • Use smem -s pss -r instead of ps aux --sort=-%mem when you need accurate per-process memory figures — especially on servers with many processes sharing libraries.
  • Install sysstat on every production server immediately after provisioning to enable historical sar data collection.
  • Set vm.swappiness=10 persistently in /etc/sysctl.conf on database servers to minimize swap usage.
  • Check /proc/meminfo for Slab and Dirty values when standard tools do not explain observed memory pressure.
  • For containerized workloads, read cgroup memory files — not free — for accurate limits and usage.
  • On shared hosting or VPS environments, establish memory baselines during normal operation so anomalies are immediately recognizable.

Frequently Asked Questions

What is the difference between "free" and "available" memory in Linux?

"Free" is RAM with no current use at all. "Available" is the realistic estimate of memory that can be allocated to new processes without triggering swapping — it includes reclaimable page cache and buffers. On a healthy Linux server, "free" is often near zero while "available" remains high. Always use "available" for capacity assessments.

Why does the sum of all process RSS values exceed total physical RAM?

RSS (Resident Set Size) counts shared memory — such as shared libraries — once per process that maps it. If 50 processes each map a 100 MB shared library, the total RSS sum inflates by 4,900 MB beyond the actual physical cost. Use smem with PSS reporting to eliminate this double-counting.

How do I find which process triggered the Linux OOM killer?

Run dmesg | grep -i "oom|killed process" or journalctl -k | grep -i oom. The kernel logs the exact process name, PID, and memory statistics at the moment of the kill event, including the oom_score of all processes evaluated.

What does high swap usage indicate, and how serious is it?

Sustained swap usage means the system's working set exceeds physical RAM. The kernel compensates by paging data to disk, which is typically 100–1,000x slower than RAM access. Even modest swap activity causes measurable application latency. It is a definitive signal to either reduce memory consumption, increase RAM, or redistribute workloads.

Can I monitor RAM usage inside a Docker container using standard Linux commands?

Standard commands like free inside a container show the host's total RAM, not the container's cgroup limit. For accurate container-level metrics, read /sys/fs/cgroup/memory/memory.usage_in_bytes for current usage and /sys/fs/cgroup/memory/memory.limit_in_bytes for the configured limit. Alternatively, use docker stats <container_name> from the host for a formatted view.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started