Introduction / Why This Matters
Continuous monitoring of RAM usage is a critical task for Linux administrators and developers deploying applications. Uncontrolled memory consumption can lead to system slowdowns, heavy swap partition usage, and in the worst case—process termination by the OOM Killer. This guide will help you master a set of built-in and third-party tools for quickly assessing memory status, identifying "memory-hungry" processes, and making informed decisions about optimization.
Prerequisites / Preparation
Before you begin, ensure that:
- You have access to a Linux terminal (via SSH or locally).
- You have permissions to execute commands (usually a regular user is sufficient, but
sudorights are required to install additional packages). - Most tools (
free,top,vmstat,/proc/meminfo) are pre-installed on any Linux distribution. - To install
htop,nmon, orglances, you will need a package manager (apt,yum,dnf).
Step 1: Using the free Command
The free command is the fastest way to get a summary of RAM and swap usage.
free -h
The -h (human-readable) flag outputs values in convenient units (MB, GB). Example output:
total used free shared buff/cache available
Mem: 7.7G 2.1G 3.2G 156M 2.4G 5.1G
Swap: 2.0G 0B 2.0G
Pay attention to the columns:
- used: memory used by processes (excluding caches).
- free: completely unused memory.
- buff/cache: memory used for buffering and caching (the kernel can free this if needed).
- available: an estimate of memory available for starting new processes without using swap. This is the most important metric.
Step 2: Interactive Monitoring with top and htop
To monitor dynamics and identify processes consuming large amounts of memory, use top or the more user-friendly alternative htop.
Using top
Run:
top
By default, top sorts by CPU load. To sort by memory, press F (or f), then select the %MEM column (or RES for physical memory) and press s to save. Press q to exit.
Installing and Using htop
htop provides a colorful interface with convenient navigation:
# For Ubuntu/Debian:
sudo apt update && sudo apt install htop
# For CentOS/RHEL/Fedora:
sudo yum install htop # or sudo dnf install htop
Launch htop. Memory is displayed as colored bars at the top (blue — used, green — buff/cache). To sort by memory, press F6 and select PERCENT_CPU or RES, or simply click a column header (if mouse support is enabled). Processes using the most memory will appear at the top.
Step 3: Virtual Memory Statistics with vmstat
vmstat provides overall system statistics, including memory, swap, and I/O operations.
vmstat -s
The output will show detailed figures, for example:
8192000 K total memory
2150400 K used memory
3901440 K active memory
1324544 K inactive memory
3176448 K free memory
123456 K buffer memory
2048000 K swap cache
2097152 K total swap
0 K used swap
2097152 K free swap
For continuous monitoring with a 2-second interval:
vmstat 2
Watch the si (swap in) and so (swap out) columns. If they are consistently non-zero, it's a sign of insufficient RAM.
Step 4: Detailed Information from /proc/meminfo
The /proc/meminfo file contains the most detailed, low-level memory data. It's a plain text file you can read directly:
cat /proc/meminfo
Example output (abbreviated):
MemTotal: 8192000 kB
MemFree: 3176448 kB
MemAvailable: 5100000 kB
Buffers: 123456 kB
Cached: 2048000 kB
SwapCached: 0 kB
Active: 3901440 kB
Inactive: 1324544 kB
SwapTotal: 2097152 kB
SwapFree: 2097152 kB
Here:
- MemAvailable — the most accurate estimate of available memory (accounts for caches and buffers that can be freed quickly).
- Cached — memory used by the page cache (to speed up disk reads).
- Buffers — memory for block device buffering (e.g., disks).
To filter specific fields, use grep:
grep -E 'MemTotal|MemFree|MemAvailable|Buffers|Cached' /proc/meminfo
Step 5: Advanced Utilities nmon and glances
For comprehensive system monitoring, including memory, CPU, disks, and network, the nmon and glances utilities are convenient.
Installing nmon
# Ubuntu/Debian:
sudo apt install nmon
# CentOS/RHEL:
sudo yum install nmon
Launch nmon. Press m to switch to the memory screen. There you'll see a detailed breakdown of memory types, as well as real-time graphs. Press q to exit.
Installing glances
glances is a cross-platform, text-mode monitor with a web interface option.
# Installation via pip (recommended):
pip3 install glances
# Or via package manager (may be outdated):
sudo apt install glances # Ubuntu/Debian
Launch glances. By default, it displays all metrics, including memory (top left). Press h for help on navigation.
Verification
After completing the steps, you should:
- See current memory values in the output of
free -hor/proc/meminfo. Theavailable(orMemAvailable) field should be reasonable (not close to zero). - In
top/htop, confirm there are no processes constantly consuming more memory than expected. - When using
vmstat, check thatsiandsoare close to zero (if swap is constantly active—this is a warning sign). - In
nmon/glances, assess the overall picture and trends.
If memory is running out, you'll see used increasing and free/available decreasing, along with increased swap activity (si, so).
Potential Issues
1. Error "command not found" for htop/nmon/glances
Solution: Install the missing package via your distribution's package manager (see steps above). Ensure you use sudo for installation.
2. Insufficient permissions to view process information
Solution: Commands like free, vmstat, and cat /proc/meminfo are generally accessible to all. However, viewing all processes in top/htop (especially others' processes) may require running with sudo. This is not necessary for a general memory overview, though.
3. Misinterpreting free output (confusion with cached/buffers)
Solution: Do not rely on free and used columns from older versions of free. Always look at available (or calculate: free + buffers + cached). The Linux kernel actively uses free memory for caching, which speeds up the system, and will free it when needed.
4. High memory usage but no obvious consuming processes
Solution: Memory may be used by the kernel (e.g., for slab allocators). Check the Slab and KernelStack fields in /proc/meminfo. There could also be a leak in a kernel driver or module. In such cases, a reboot or system update may help.
5. Utilities showing different information
Solution: Different tools may use different units of measurement (kB vs. KiB) or count caches differently. Always cross-reference with the "true source"—/proc/meminfo.