Types of Memory in Linux
| Type | Description |
|---|---|
| Physical (RAM) | Actual hardware memory installed |
| Virtual | Abstraction layer mapping to physical addresses |
| Swap | Disk space used when RAM is full |
| Cache/Buffers | RAM used to speed up disk operations |
Checking Memory Usage
The free Command
free -hOutput:
total used free shared buff/cache available
Mem: 3.8Gi 425Mi 3.1Gi 0.0Ki 246Mi 3.1Gi
Swap: 0B 0B 0B
| Column | Meaning |
|---|---|
| total | Total installed RAM |
| used | RAM actively in use |
| free | Completely unused RAM |
| buff/cache | RAM used for caching (reclaimable) |
| available | RAM available for new processes |
Key insight: Linux uses free RAM for caching to speed up operations. The available column shows what's truly available — not free.
The /proc/meminfo File
For detailed memory statistics:
# Overview
cat /proc/meminfo | grep "Mem"
# Swap info
cat /proc/meminfo | grep "Swap"
# Buffer and cache details
cat /proc/meminfo | grep -e "Buffers" -e "Cached"Clearing Caches
In rare cases, you may need to free cached memory:
# Sync pending writes to disk first
sync
# Clear page cache, dentries, and inodes
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
# Verify
free -hClearing caches is rarely needed in production. Linux manages memory efficiently — caches improve performance and are released automatically when applications need RAM.
Swap Management
Swap is disk space used as overflow when RAM is exhausted. It's much slower than RAM.
Check Swap Status
# View swap usage
free -h | grep Swap
# Detailed swap info
swapon --showCreate a Swap File
# Create a 2GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Verify
free -h
# Make persistent (add to /etc/fstab)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabSwappiness
Controls how aggressively Linux uses swap (0-100):
# Check current value
cat /proc/sys/vm/swappiness
# Set to 10 (prefer RAM, use swap only when necessary)
sudo sysctl -w vm.swappiness=10
# Make persistent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.confMonitoring with vmstat
vmstat provides a snapshot of system performance:
# Single snapshot
vmstat
# Update every 2 seconds, 10 times
vmstat 2 10
# With timestamps
vmstat -t 2 5
# Memory summary
vmstat -sKey columns:
| Column | Meaning |
|---|---|
| r | Processes waiting for CPU |
| b | Processes in uninterruptible sleep |
| swpd | Virtual memory used (swap) |
| free | Free RAM |
| si/so | Swap in/out (high = problem) |
| us | User CPU time |
| sy | System CPU time |
| id | Idle CPU time |
| wa | Waiting for I/O |
Red flags: High si/so means excessive swapping. High wa means disk bottleneck.
Process Management for Performance
Finding Resource-Hungry Processes
# Top CPU consumers
ps aux --sort=-%cpu | head -10
# Top memory consumers
ps aux --sort=-%mem | head -10
# Real-time monitoring
top
# Press M to sort by memory, P to sort by CPUKilling Problematic Processes
# Find the PID
ps aux | grep "problematic_app"
# Graceful termination
kill PID
# Force kill if unresponsive
kill -9 PIDProcess Priority (nice/renice)
Adjust CPU priority for processes:
# Start a process with low priority (higher nice = lower priority)
nice -n 19 ./heavy_computation.sh
# Change priority of running process
renice -n 10 -p PIDNice values range from -20 (highest priority) to 19 (lowest priority).
Practical: Server Health Script
#!/bin/bash
# server_health.sh
echo "=== Memory Status ==="
free -h | grep Mem | awk '{printf "Used: %s / Total: %s (Available: %s)\n", $3, $2, $7}'
echo ""
echo "=== Swap Status ==="
free -h | grep Swap | awk '{printf "Used: %s / Total: %s\n", $3, $2}'
echo ""
echo "=== Top 5 Memory Consumers ==="
ps aux --sort=-%mem | head -6 | awk '{printf "%-10s %5s%% %s\n", $1, $4, $11}'
echo ""
echo "=== CPU Load (1/5/15 min) ==="
uptime | awk -F'load average:' '{print $2}'Summary
free -hshows memory overview;availableis the key metric- Linux caches aggressively — this is normal and beneficial
- Swap is emergency overflow; high swap usage indicates insufficient RAM
vmstatmonitors system performance over time- High
si/so= swap thrashing, highwa= disk bottleneck - Use
ps aux --sort=-%memto find memory hogs nice/reniceadjust process CPU priority
Next Steps
Next, we'll cover storage management — partitioning disks, creating file systems, and managing mount points.