A bash script that shows your RAM usage—top processes, caches and kernel-level allocations.
#!/bin/bash
# RAM Usage Analysis Script - Enhanced with Colors
# Colors only work in ANSI-compatible terminals
# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color / Reset
echo -e "${BLUE}=== RAM USAGE ANALYSIS ===${NC}"
# Total RAM summary
echo -e "\n${YELLOW}RAM SUMMARY:${NC}"
free -h | grep "^Mem:" | awk -v G="$GREEN" -v Y="$YELLOW" -v NC="$NC" '{
print " Total: " $2
print " Used: " Y $3 NC
print " Free: " G $4 NC
print " Available: " G $7 NC
}'
# Top 10 processes by RAM usage (with PID)
echo -e "\n${YELLOW}TOP 10 PROCESSES BY RAM USAGE:${NC}"
ps aux --sort=-%mem | head -11 | awk -v Y="$YELLOW" -v NC="$NC" '
NR==1 {
printf "%-8s %-6s %5s%% %8s %s\n", $1, $2, $4, $6, $11
}
NR>1 && NR<=11 {
printf "%-8s %-6s %5s%% %8s %s\n", $1, $2, Y $4 NC, $6, $11
}'
# Memory usage by category - top application processes (>1%)
echo -e "\n${YELLOW}MEMORY BY CATEGORY:${NC}"
echo -e "${GREEN}Applications & Services (top users >1%):${NC}"
ps aux --sort=-%mem | awk -v G="$GREEN" -v NC="$NC" '
NR>1 && $4 > 1.0 {
printf " %-10s %5s%% %s\n", $1, $4, $11
}' | head -10
echo -e "\n${GREEN}System & Kernel:${NC}"
awk -v G="$GREEN" -v NC="$NC" '
/^(MemTotal|MemFree|MemAvailable|Buffers|Cached|Slab)/ {
gsub(/kB/,""); val = sprintf("%.0f", $2 / 1024);
if ($1 ~ /Buffers/) print " Buffers: " G val " MB" NC;
if ($1 ~ /Cached/) print " Cached: " G val " MB" NC;
if ($1 ~ /Slab/) print " Slab (kernel): " G val " MB" NC;
}' /proc/meminfo
echo -e "\n${YELLOW}QUICK TIP:${NC}"
echo "Available RAM includes cached memory that can be freed if needed."
echo -e "${RED}To clear caches (requires sudo):${NC}"
echo " sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches"
echo -e "\n${BLUE}=== END ANALYSIS ===${NC}"
Bash Scripting Series - Ben Santora - January 2026
Top comments (0)