Memory Monitoring — Monitoring RAM and Swap Usage in Linux¶
Memory Monitoring is the process of tracking RAM utilization, swap usage, memory allocation, cache usage, and memory-intensive processes to ensure Linux systems operate efficiently. Insufficient available memory can lead to application slowdowns, excessive swapping, Out of Memory (OOM) events, and system instability. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, and Site Reliability Engineer (SRE) should understand how to monitor memory usage and troubleshoot memory-related issues in production environments.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand Linux memory management
- Monitor RAM and swap usage
- Interpret memory statistics
- Identify memory-intensive processes
- Monitor memory performance
- Detect memory bottlenecks
- Troubleshoot memory issues
- Apply production monitoring best practices
Prerequisites¶
Complete:
- Modules 1–11
- Module 12 Lessons 1–5
Why Learn Memory Monitoring?¶
Imagine a production application server.
Without monitoring:
With monitoring:
Monitoring memory helps prevent application failures.
Linux Memory Overview¶
Linux uses memory for:
- Running applications
- Kernel operations
- File system cache
- Buffers
- Shared memory
- Swap
Unused memory is often used for caching to improve performance.
Memory Architecture¶
RAM is significantly faster than swap storage.
Check Memory Usage¶
The most common command:
Example:
Understanding free Output¶
| Column | Description |
|---|---|
| total | Total RAM |
| used | Memory currently in use |
| free | Completely unused RAM |
| shared | Shared memory |
| buff/cache | Memory used for buffers and filesystem cache |
| available | Estimated memory available for new applications |
Important
On Linux, a low free value is normal. Focus on the available column, which better represents how much memory can be allocated without swapping.
Check Memory in Megabytes¶
Check Memory in Gigabytes¶
Monitor Memory Continuously¶
Refresh every two seconds.
Useful during troubleshooting.
View Memory Statistics¶
Use:
Example:
Refresh every two seconds.
Important columns:
- Free memory
- Buffers
- Cache
- Swap activity
- CPU usage
Monitor Memory with top¶
Memory summary:
Press:
Sort processes by memory usage.
Monitor Memory with htop¶
Benefits:
- Colorized display
- Memory graphs
- Interactive interface
- Easy process management
Find Memory-Intensive Processes¶
Using ps:
Top memory consumers:
View Process Memory Usage¶
Use:
Example:
Summary only.
Monitor Swap Usage¶
Display swap.
Or:
Excessive swap usage often indicates memory pressure.
Check OOM Events¶
The Linux kernel logs Out of Memory events.
View:
Or:
Virtual Memory Statistics¶
Detailed information.
Example:
Common Commands¶
Memory usage.
Memory statistics.
Process monitoring.
Largest memory consumers.
Kernel OOM messages.
Real Production Examples¶
Check available memory.
Monitor every second.
Find top memory consumers.
Check OOM events.
Production Perspective¶
Memory monitoring is essential for:
- Kubernetes nodes
- Database servers
- Java applications
- Python services
- Web servers
- Cloud virtual machines
- CI/CD servers
- Enterprise applications
Memory exhaustion is one of the most common causes of application instability.
Hands-on Lab¶
Task 1¶
Display memory usage.
Task 2¶
Refresh memory usage continuously.
Task 3¶
Display virtual memory statistics.
Task 4¶
Open top.
Press:
Sort by memory usage.
Task 5¶
Identify the largest memory-consuming processes.
Task 6¶
Display detailed memory information.
Task 7¶
Display swap usage.
Task 8¶
Search for Out of Memory events.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
free -h | Display memory usage | Capacity monitoring |
vmstat | Virtual memory statistics | Performance analysis |
top | Real-time process monitoring | Troubleshooting |
htop | Interactive monitoring | System administration |
ps aux --sort=-%mem | Largest memory consumers | Memory leak detection |
cat /proc/meminfo | Detailed memory information | Low-level diagnostics |
Common Memory Monitoring Mistakes¶
| Mistake | Solution |
|---|---|
Focusing only on the free column | Monitor the available column as well |
| Ignoring swap usage | Investigate sustained swap activity |
| Never checking OOM events | Review kernel logs regularly |
| Looking only at total memory | Identify individual memory-consuming processes |
| Assuming high cache usage is always a problem | Linux uses available RAM efficiently for caching |
Production Troubleshooting Scenario¶
Scenario
A production API server becomes slow.
Investigation:
Result:
Next:
A Java process is consuming most of the RAM.
Further investigation confirms a memory leak.
After tuning the application and restarting the service:
Best Practices¶
- Monitor available memory instead of only free memory.
- Configure alerts before memory usage becomes critical.
- Monitor swap usage regularly.
- Investigate repeated OOM events immediately.
- Identify long-running memory-intensive processes.
- Monitor application memory trends over time.
- Combine memory monitoring with CPU and disk metrics.
- Use centralized monitoring tools such as Prometheus and Grafana for historical analysis.
Common Mistakes¶
❌ Assuming low free memory always indicates a problem.
✅ Verify low free memory always indicates a problem instead of assuming it.
❌ Ignoring swap activity.
✅ Always review swap activity.
❌ Never checking kernel OOM events.
✅ Always checking kernel OOM events.
❌ Monitoring only total memory usage.
✅ Avoid this mistake: monitoring only total memory usage.
❌ Ignoring gradual memory growth that may indicate a memory leak.
✅ Always review gradual memory growth that may indicate a memory leak.
Interview Questions¶
Beginner¶
- What does
free -hdisplay? - What is swap memory?
- Which command displays virtual memory statistics?
- What is the purpose of
vmstat?
Intermediate¶
- What is the difference between
used,free, andavailablememory? - How do you identify memory-intensive processes?
- What causes Linux to use swap?
- How do you investigate an OOM event?
Architect Level¶
- How would you monitor memory across hundreds of Linux servers?
- How would you troubleshoot a Kubernetes node experiencing memory pressure?
- How would you distinguish between normal cache usage and an application memory leak?
Summary¶
In this lesson, you learned:
- Linux memory management
- RAM and swap monitoring
- Memory statistics
- Process memory analysis
- OOM event investigation
- Memory troubleshooting
- Performance monitoring
- Production best practices
Memory monitoring is a critical part of maintaining healthy Linux systems. By tracking RAM usage, swap activity, memory-intensive processes, and kernel OOM events, administrators can identify bottlenecks early, optimize application performance, and prevent production outages caused by memory exhaustion.
Key Takeaways¶
- Use
free -hto monitor memory utilization. - Focus on the available memory column rather than only free memory.
- Monitor swap usage and investigate sustained swapping.
- Use
vmstat,top, andhtopfor deeper analysis. - Identify high-memory processes using
ps. - Review OOM events to diagnose memory-related failures.
What's Next?¶
CPU Monitoring — Monitoring Processor Performance in Linux
You'll explore:
- Understanding CPU utilization
- Monitoring CPU performance
- Load average
- Using
top,uptime,vmstat, and related tools - Identifying CPU-intensive processes
- Troubleshooting CPU bottlenecks
- Production CPU monitoring best practices
By the end of the lesson, you'll be able to monitor CPU performance, analyze system load, identify resource-intensive processes, and troubleshoot CPU-related performance issues in production Linux environments.