free Command
The free command displays memory usage information in Linux and Unix systems. It shows the total amount of physical and swap memory, as well as how much is currently used, free, and available for new processes, making it essential for system monitoring and performance analysis.
Syntax
Description
The free command provides a snapshot of system memory usage, including physical RAM and swap space. It displays information about total memory, used memory, free memory, shared memory, buffer/cache memory, and available memory for new applications.
Key features:
- Display physical memory (RAM) and swap space usage
- Show memory in various units (bytes, KB, MB, GB)
- Continuous monitoring with automatic updates
- Calculate available memory for new processes
- Monitor memory pressure and swap usage
Memory Fields Explanation
- total: Total installed physical memory
- used: Memory currently in use by processes
- free: Completely unused memory
- shared: Memory used by tmpfs filesystems
- buff/cache: Memory used for buffers and cache
- available: Memory available for new processes (most important)
Understanding Memory Types
- Physical Memory (RAM): Fast memory directly accessible by CPU
- Swap Space: Disk space used when RAM is full (much slower)
- Buffer Memory: Temporary storage for I/O operations
- Cache Memory: Recently accessed files kept in memory
- Available Memory: Free + reclaimable buffer/cache memory
Sample free -h Output
total used free shared buff/cache available Mem: 7.8Gi 2.1Gi 3.2Gi 256Mi 2.5Gi 5.2Gi Swap: 2.0Gi 0B 2.0Gi
Examples
Basic memory checking
free -h # Human-readable format
free -m # Display in megabytes
free -g # Display in gigabytes
Check memory usage in different units and formats
Continuous monitoring
free -h -s 1 -c 10 # Update 10 times, every 1 second
watch -n 2 'free -h' # Alternative using watch command
Monitor memory usage changes over time
Detailed memory information
free -h -w # Wide mode (separate buffers/cache)
free -h -t -w # Both total and wide mode
Get more detailed breakdown of memory usage
Memory analysis and scripting
free | awk 'NR==2{printf "%.2f%%\n", $7/$2*100}'
# Check if memory usage is high
free | awk 'NR==2{if($3/$2 > 0.8) print "High memory usage!"}'
# Get swap usage
free -h | grep Swap | awk '{print "Swap: " $3 "/" $2}'
Extract specific memory metrics for analysis and alerting
Memory pressure detection
free -h | grep Swap | awk '$3 != "0B" {print "Swap in use: " $3}'
# Monitor available memory
while true; do
avail=$(free -m | awk 'NR==2{print $7}')
if [ $avail -lt 500 ]; then
echo "Low memory warning: ${avail}MB available"
fi
sleep 30
done
Detect memory pressure and potential performance issues
⚠️ Memory Usage Warnings
- High swap usage: Indicates memory pressure, can cause severe performance degradation
- Low available memory: Less than 10% available may cause system slowdown
- OOM (Out of Memory): System may kill processes when memory is exhausted
- Buffer/cache confusion: High used memory is often just cache and is normal
- Focus on 'available': This is the most important metric for new processes
💡 Tips and Best Practices
- Use -h for readability: Much easier to interpret than raw bytes
- Focus on 'available' column: This shows memory actually available for new processes
- Don't panic about high 'used': Linux uses memory for cache, which is good
- Monitor swap usage: Any significant swap usage indicates memory pressure
- Combine with top/htop: Identify which processes are using memory
- Regular monitoring: Set up alerts for low available memory
Common Use Cases
- Quick memory check:
free -h - Monitor memory continuously:
free -h -s 5 - Check swap usage:
free -h | grep Swap - Memory health check:
free -h -t - Script memory monitoring:
free -m | awk 'NR==2{print $7}' - Performance troubleshooting:
free -h -w