Master the free -h Command: Monitor Memory Usage in Linux
Check memory and swap usage with free -h
!
What is the free -h Command?
Picture free -h
as your system’s memory gauge, showing how much RAM and swap space is used or available in a human-readable format (e.g., GB, MB). It’s key for diagnosing performance issues.
Why free -h is Essential
Memory Monitoring
Track RAM and swap usage.
Performance Diagnosis
Identify memory bottlenecks.
Server Management
Monitor memory on servers.
Syntax and Options
The free -h
command displays memory usage:
free -h
Key options:
-h
: Human-readable sizes (e.g., GB, MB).-s
: Continuous updates (seconds).-t
: Show total for RAM and swap.-w
: Wide output withbuff/cache
split.
Output columns: total
, used
, free
, shared
, buff/cache
, available
.
Real-World Examples
1. Check Memory Usage
View memory status:
free -h
Output: Shows RAM and swap (e.g., Mem: 16G 8G 4G 1G 4G
).
2. Include Totals
Show combined RAM and swap:
free -h -t
Output: Adds a total
row for combined memory.
3. Continuous Monitoring
Update every 5 seconds:
free -h -s 5
Output: Refreshes memory stats every 5 seconds.
4. Wide Output
Use wide format for detailed buff/cache
:
free -h -w
Output: Splits buff/cache
into separate columns.
5. Check Low Memory
Monitor available memory:
free -h | grep Mem | awk '{print $7}'
Output: Shows available
memory (e.g., 4G
).
Advanced Usage
Elevate free -h
with these techniques:
- Scripting Alerts: Use in scripts to warn about low memory.
- Continuous Monitoring: Combine with
watch
for real-time updates. - Compare with top: Cross-check memory usage with
top
. - Extract Specific Metrics: Use
awk
to parse available memory.
Example: Alert for low memory:
free -h | grep Mem | awk '$7 < 1G {print "Low memory: " $7}'
Pro Tips
Check Available: Focus on available
for usable memory.
Monitor Swap: High swap usage may indicate RAM shortages.
Automate: Use watch free -h
for continuous monitoring.
Common Mistakes to Avoid
Avoid these pitfalls with free -h
:
- Misreading Free: Focus on
available
, notfree
, for usable memory. - Ignoring Swap: High swap usage can slow performance.
- Skipping -h: Use
-h
for readable units.
Explore More Linux Commands
Master Linux with our expert guides!