Loading...
Monitors memory usage and alerts when thresholds are exceeded
{
"hookConfig": {
"hooks": {
"notification": {
"script": "./.claude/hooks/memory-usage-monitor.sh",
"timeout": 5000
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\n# Memory Usage Monitor Hook\n# Monitors system and process memory usage during development activities\n\necho \"💾 Memory Usage Monitor\" >&2\n\n# Initialize monitoring variables\nTOTAL_MEMORY_KB=0\nUSED_MEMORY_KB=0\nMEMORY_PERCENT=0\nSWAP_PERCENT=0\nHIGH_MEMORY_THRESHOLD=75\nCRITICAL_MEMORY_THRESHOLD=90\nHIGH_SWAP_THRESHOLD=25\nWARNINGS=0\nERRORS=0\n\n# Function to report memory status\nreport_memory() {\n local level=\"$1\"\n local message=\"$2\"\n \n case \"$level\" in\n \"ERROR\")\n echo \"❌ CRITICAL: $message\" >&2\n ERRORS=$((ERRORS + 1))\n ;;\n \"WARNING\")\n echo \"⚠️ WARNING: $message\" >&2\n WARNINGS=$((WARNINGS + 1))\n ;;\n \"INFO\")\n echo \"ℹ️ INFO: $message\" >&2\n ;;\n \"PASS\")\n echo \"✅ OK: $message\" >&2\n ;;\n esac\n}\n\n# Function to format bytes to human readable\nformat_bytes() {\n local bytes=$1\n local units=(\"B\" \"KB\" \"MB\" \"GB\" \"TB\")\n local unit=0\n \n while (( bytes > 1024 && unit < 4 )); do\n bytes=$((bytes / 1024))\n unit=$((unit + 1))\n done\n \n echo \"${bytes}${units[$unit]}\"\n}\n\n# 1. System Memory Analysis\necho \"📊 Analyzing system memory usage...\" >&2\n\n# Detect operating system for platform-specific commands\nOS_TYPE=$(uname -s)\n\ncase \"$OS_TYPE\" in\n \"Linux\")\n # Linux memory information\n if [ -f /proc/meminfo ]; then\n TOTAL_MEMORY_KB=$(grep '^MemTotal:' /proc/meminfo | awk '{print $2}')\n AVAILABLE_MEMORY_KB=$(grep '^MemAvailable:' /proc/meminfo | awk '{print $2}' || echo \"0\")\n \n if [ \"$AVAILABLE_MEMORY_KB\" -eq 0 ]; then\n # Fallback calculation for older systems\n FREE_MEMORY_KB=$(grep '^MemFree:' /proc/meminfo | awk '{print $2}')\n BUFFERS_KB=$(grep '^Buffers:' /proc/meminfo | awk '{print $2}')\n CACHED_KB=$(grep '^Cached:' /proc/meminfo | awk '{print $2}')\n AVAILABLE_MEMORY_KB=$((FREE_MEMORY_KB + BUFFERS_KB + CACHED_KB))\n fi\n \n USED_MEMORY_KB=$((TOTAL_MEMORY_KB - AVAILABLE_MEMORY_KB))\n MEMORY_PERCENT=$((USED_MEMORY_KB * 100 / TOTAL_MEMORY_KB))\n \n echo \" 🖥️ Total Memory: $(format_bytes $((TOTAL_MEMORY_KB * 1024)))\" >&2\n echo \" 📈 Used Memory: $(format_bytes $((USED_MEMORY_KB * 1024))) ($MEMORY_PERCENT%)\" >&2\n \n # Check swap usage\n if [ -f /proc/swaps ]; then\n SWAP_TOTAL_KB=$(awk 'NR>1 {sum+=$3} END {print sum+0}' /proc/swaps)\n SWAP_USED_KB=$(awk 'NR>1 {sum+=$4} END {print sum+0}' /proc/swaps)\n \n if [ \"$SWAP_TOTAL_KB\" -gt 0 ]; then\n SWAP_PERCENT=$((SWAP_USED_KB * 100 / SWAP_TOTAL_KB))\n echo \" 💿 Swap Usage: $(format_bytes $((SWAP_USED_KB * 1024))) / $(format_bytes $((SWAP_TOTAL_KB * 1024))) ($SWAP_PERCENT%)\" >&2\n else\n echo \" 💿 Swap: Not configured\" >&2\n fi\n fi\n else\n report_memory \"WARNING\" \"Unable to read /proc/meminfo - memory monitoring limited\"\n fi\n ;;\n \n \"Darwin\")\n # macOS memory information\n if command -v vm_stat &> /dev/null; then\n # Get page size\n PAGE_SIZE=$(vm_stat | grep 'page size of' | awk '{print $8}' || echo \"4096\")\n \n # Get memory statistics\n VM_STAT_OUTPUT=$(vm_stat)\n PAGES_FREE=$(echo \"$VM_STAT_OUTPUT\" | grep 'Pages free:' | awk '{print $3}' | tr -d '.')\n PAGES_ACTIVE=$(echo \"$VM_STAT_OUTPUT\" | grep 'Pages active:' | awk '{print $3}' | tr -d '.')\n PAGES_INACTIVE=$(echo \"$VM_STAT_OUTPUT\" | grep 'Pages inactive:' | awk '{print $3}' | tr -d '.')\n PAGES_SPECULATIVE=$(echo \"$VM_STAT_OUTPUT\" | grep 'Pages speculative:' | awk '{print $3}' | tr -d '.' || echo \"0\")\n PAGES_WIRED=$(echo \"$VM_STAT_OUTPUT\" | grep 'Pages wired down:' | awk '{print $4}' | tr -d '.')\n \n # Calculate memory usage\n TOTAL_PAGES=$((PAGES_FREE + PAGES_ACTIVE + PAGES_INACTIVE + PAGES_SPECULATIVE + PAGES_WIRED))\n USED_PAGES=$((PAGES_ACTIVE + PAGES_INACTIVE + PAGES_SPECULATIVE + PAGES_WIRED))\n TOTAL_MEMORY_KB=$((TOTAL_PAGES * PAGE_SIZE / 1024))\n USED_MEMORY_KB=$((USED_PAGES * PAGE_SIZE / 1024))\n MEMORY_PERCENT=$((USED_MEMORY_KB * 100 / TOTAL_MEMORY_KB))\n \n echo \" 🖥️ Total Memory: $(format_bytes $((TOTAL_MEMORY_KB * 1024)))\" >&2\n echo \" 📈 Used Memory: $(format_bytes $((USED_MEMORY_KB * 1024))) ($MEMORY_PERCENT%)\" >&2\n \n # Check swap usage on macOS\n if command -v sysctl &> /dev/null; then\n SWAP_USAGE=$(sysctl vm.swapusage 2>/dev/null | grep -oE 'used = [0-9.]+[KMGT]?' | awk '{print $3}' || echo \"0\")\n SWAP_TOTAL=$(sysctl vm.swapusage 2>/dev/null | grep -oE 'total = [0-9.]+[KMGT]?' | awk '{print $3}' || echo \"0\")\n echo \" 💿 Swap Usage: $SWAP_USAGE / $SWAP_TOTAL\" >&2\n fi\n else\n report_memory \"WARNING\" \"vm_stat command not available - memory monitoring limited\"\n fi\n ;;\n \n *)\n report_memory \"WARNING\" \"Unsupported operating system ($OS_TYPE) - limited memory monitoring\"\n ;;\nesac\n\n# 2. Process-Specific Memory Analysis\necho \"🔍 Analyzing development process memory usage...\" >&2\n\n# Define process patterns for different development environments\nDEV_PROCESSES=(\"node\" \"python\" \"java\" \"ruby\" \"php\" \"go\" \"rust\" \"dotnet\" \"code\" \"claude\")\nTOTAL_DEV_MEMORY_KB=0\nPROCESS_COUNT=0\n\nfor process in \"${DEV_PROCESSES[@]}\"; do\n if command -v pgrep &> /dev/null; then\n # Use pgrep for more accurate process detection\n PIDS=$(pgrep -f \"$process\" 2>/dev/null || true)\n \n if [ -n \"$PIDS\" ]; then\n PROCESS_MEMORY=0\n PROC_COUNT=0\n \n # Calculate memory for all matching processes\n for pid in $PIDS; do\n if [ -f \"/proc/$pid/status\" ]; then\n # Linux: Read from /proc/pid/status\n PID_MEMORY=$(grep '^VmRSS:' \"/proc/$pid/status\" 2>/dev/null | awk '{print $2}' || echo \"0\")\n elif command -v ps &> /dev/null; then\n # macOS/Other: Use ps command\n PID_MEMORY=$(ps -o rss= -p \"$pid\" 2>/dev/null | awk '{print $1}' || echo \"0\")\n else\n PID_MEMORY=0\n fi\n \n PROCESS_MEMORY=$((PROCESS_MEMORY + PID_MEMORY))\n PROC_COUNT=$((PROC_COUNT + 1))\n done\n \n if [ \"$PROCESS_MEMORY\" -gt 0 ]; then\n echo \" 🔧 $process processes: $PROC_COUNT running, $(format_bytes $((PROCESS_MEMORY * 1024))) memory\" >&2\n TOTAL_DEV_MEMORY_KB=$((TOTAL_DEV_MEMORY_KB + PROCESS_MEMORY))\n PROCESS_COUNT=$((PROCESS_COUNT + PROC_COUNT))\n fi\n fi\n fi\ndone\n\nif [ \"$TOTAL_DEV_MEMORY_KB\" -gt 0 ]; then\n DEV_MEMORY_PERCENT=$((TOTAL_DEV_MEMORY_KB * 100 / TOTAL_MEMORY_KB))\n echo \" 📊 Total dev processes: $PROCESS_COUNT processes, $(format_bytes $((TOTAL_DEV_MEMORY_KB * 1024))) ($DEV_MEMORY_PERCENT% of system)\" >&2\nelse\n echo \" 📋 No development processes detected\" >&2\nfi\n\n# 3. Container Memory Monitoring (if applicable)\necho \"🐳 Checking container memory usage...\" >&2\n\nif command -v docker &> /dev/null && docker info >/dev/null 2>&1; then\n # Check Docker container memory usage\n RUNNING_CONTAINERS=$(docker ps --format \"table {{.Names}}\" --no-trunc 2>/dev/null | tail -n +2 | wc -l || echo \"0\")\n \n if [ \"$RUNNING_CONTAINERS\" -gt 0 ]; then\n echo \" 🐳 Docker containers: $RUNNING_CONTAINERS running\" >&2\n \n # Get container memory stats (basic)\n CONTAINER_STATS=$(docker stats --no-stream --format \"table {{.Container}}\\t{{.MemUsage}}\" 2>/dev/null | tail -n +2 | head -5 || true)\n \n if [ -n \"$CONTAINER_STATS\" ]; then\n echo \" 📊 Top container memory usage:\" >&2\n echo \"$CONTAINER_STATS\" | while read line; do\n echo \" $line\" >&2\n done\n fi\n else\n echo \" 🐳 No running Docker containers\" >&2\n fi\nelse\n echo \" 🐳 Docker not available or not running\" >&2\nfi\n\n# 4. Memory Threshold Analysis\necho \"⚠️ Analyzing memory thresholds...\" >&2\n\n# Check system memory thresholds\nif [ \"$MEMORY_PERCENT\" -ge \"$CRITICAL_MEMORY_THRESHOLD\" ]; then\n report_memory \"ERROR\" \"Critical system memory usage: $MEMORY_PERCENT% (threshold: $CRITICAL_MEMORY_THRESHOLD%)\"\nelif [ \"$MEMORY_PERCENT\" -ge \"$HIGH_MEMORY_THRESHOLD\" ]; then\n report_memory \"WARNING\" \"High system memory usage: $MEMORY_PERCENT% (threshold: $HIGH_MEMORY_THRESHOLD%)\"\nelse\n report_memory \"PASS\" \"System memory usage within normal range: $MEMORY_PERCENT%\"\nfi\n\n# Check swap usage\nif [ \"$SWAP_PERCENT\" -ge \"$HIGH_SWAP_THRESHOLD\" ]; then\n report_memory \"WARNING\" \"High swap usage detected: $SWAP_PERCENT% (threshold: $HIGH_SWAP_THRESHOLD%)\"\nelse\n if [ \"$SWAP_PERCENT\" -gt 0 ]; then\n report_memory \"INFO\" \"Swap usage normal: $SWAP_PERCENT%\"\n fi\nfi\n\n# Check for potential memory leaks (high dev process usage)\nif [ \"$TOTAL_DEV_MEMORY_KB\" -gt 0 ] && [ \"$DEV_MEMORY_PERCENT\" -ge 50 ]; then\n report_memory \"WARNING\" \"Development processes using significant memory: $DEV_MEMORY_PERCENT% of system\"\nelif [ \"$TOTAL_DEV_MEMORY_KB\" -gt 0 ]; then\n report_memory \"INFO\" \"Development process memory usage normal: $DEV_MEMORY_PERCENT% of system\"\nfi\n\n# 5. Memory Recommendations\necho \"💡 Memory optimization recommendations...\" >&2\n\nif [ \"$MEMORY_PERCENT\" -ge \"$HIGH_MEMORY_THRESHOLD\" ]; then\n echo \" • Consider closing unused applications and browser tabs\" >&2\n echo \" • Restart development servers to free up memory\" >&2\n echo \" • Check for memory leaks in your applications\" >&2\nfi\n\nif [ \"$SWAP_PERCENT\" -ge \"$HIGH_SWAP_THRESHOLD\" ]; then\n echo \" • High swap usage may slow down development\" >&2\n echo \" • Consider adding more RAM or closing applications\" >&2\nfi\n\nif [ \"$PROCESS_COUNT\" -gt 10 ]; then\n echo \" • Many development processes running ($PROCESS_COUNT)\" >&2\n echo \" • Consider stopping unused development servers\" >&2\nfi\n\n# 6. Generate Memory Summary\necho \"\" >&2\necho \"📋 Memory Usage Summary:\" >&2\necho \"========================\" >&2\necho \" 💾 System Memory: $MEMORY_PERCENT% used ($(format_bytes $((USED_MEMORY_KB * 1024))) / $(format_bytes $((TOTAL_MEMORY_KB * 1024))))\" >&2\n[ \"$SWAP_PERCENT\" -gt 0 ] && echo \" 💿 Swap Usage: $SWAP_PERCENT%\" >&2\necho \" 🔧 Dev Processes: $PROCESS_COUNT running ($(format_bytes $((TOTAL_DEV_MEMORY_KB * 1024))))\" >&2\necho \" ⚠️ Warnings: $WARNINGS\" >&2\necho \" ❌ Critical Issues: $ERRORS\" >&2\n\nif [ \"$ERRORS\" -eq 0 ] && [ \"$WARNINGS\" -eq 0 ]; then\n echo \" 🎉 Status: OPTIMAL - Memory usage is healthy\" >&2\nelif [ \"$ERRORS\" -eq 0 ]; then\n echo \" ✅ Status: GOOD - Memory usage acceptable with minor warnings\" >&2\nelse\n echo \" ❌ Status: CRITICAL - Memory usage requires immediate attention\" >&2\nfi\n\necho \"\" >&2\necho \"💡 Memory Best Practices:\" >&2\necho \" • Monitor memory usage regularly during development\" >&2\necho \" • Use memory profiling tools to identify leaks\" >&2\necho \" • Restart development servers periodically\" >&2\necho \" • Consider using lighter development tools when memory is limited\" >&2\necho \" • Close unused applications and browser tabs\" >&2\n\n# Memory monitoring complete\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"notification": {
"script": "./.claude/hooks/memory-usage-monitor.sh",
"timeout": 5000
}
}
}#!/usr/bin/env bash
# Memory Usage Monitor Hook
# Monitors system and process memory usage during development activities
echo "💾 Memory Usage Monitor" >&2
# Initialize monitoring variables
TOTAL_MEMORY_KB=0
USED_MEMORY_KB=0
MEMORY_PERCENT=0
SWAP_PERCENT=0
HIGH_MEMORY_THRESHOLD=75
CRITICAL_MEMORY_THRESHOLD=90
HIGH_SWAP_THRESHOLD=25
WARNINGS=0
ERRORS=0
# Function to report memory status
report_memory() {
local level="$1"
local message="$2"
case "$level" in
"ERROR")
echo "❌ CRITICAL: $message" >&2
ERRORS=$((ERRORS + 1))
;;
"WARNING")
echo "⚠️ WARNING: $message" >&2
WARNINGS=$((WARNINGS + 1))
;;
"INFO")
echo "ℹ️ INFO: $message" >&2
;;
"PASS")
echo "✅ OK: $message" >&2
;;
esac
}
# Function to format bytes to human readable
format_bytes() {
local bytes=$1
local units=("B" "KB" "MB" "GB" "TB")
local unit=0
while (( bytes > 1024 && unit < 4 )); do
bytes=$((bytes / 1024))
unit=$((unit + 1))
done
echo "${bytes}${units[$unit]}"
}
# 1. System Memory Analysis
echo "📊 Analyzing system memory usage..." >&2
# Detect operating system for platform-specific commands
OS_TYPE=$(uname -s)
case "$OS_TYPE" in
"Linux")
# Linux memory information
if [ -f /proc/meminfo ]; then
TOTAL_MEMORY_KB=$(grep '^MemTotal:' /proc/meminfo | awk '{print $2}')
AVAILABLE_MEMORY_KB=$(grep '^MemAvailable:' /proc/meminfo | awk '{print $2}' || echo "0")
if [ "$AVAILABLE_MEMORY_KB" -eq 0 ]; then
# Fallback calculation for older systems
FREE_MEMORY_KB=$(grep '^MemFree:' /proc/meminfo | awk '{print $2}')
BUFFERS_KB=$(grep '^Buffers:' /proc/meminfo | awk '{print $2}')
CACHED_KB=$(grep '^Cached:' /proc/meminfo | awk '{print $2}')
AVAILABLE_MEMORY_KB=$((FREE_MEMORY_KB + BUFFERS_KB + CACHED_KB))
fi
USED_MEMORY_KB=$((TOTAL_MEMORY_KB - AVAILABLE_MEMORY_KB))
MEMORY_PERCENT=$((USED_MEMORY_KB * 100 / TOTAL_MEMORY_KB))
echo " 🖥️ Total Memory: $(format_bytes $((TOTAL_MEMORY_KB * 1024)))" >&2
echo " 📈 Used Memory: $(format_bytes $((USED_MEMORY_KB * 1024))) ($MEMORY_PERCENT%)" >&2
# Check swap usage
if [ -f /proc/swaps ]; then
SWAP_TOTAL_KB=$(awk 'NR>1 {sum+=$3} END {print sum+0}' /proc/swaps)
SWAP_USED_KB=$(awk 'NR>1 {sum+=$4} END {print sum+0}' /proc/swaps)
if [ "$SWAP_TOTAL_KB" -gt 0 ]; then
SWAP_PERCENT=$((SWAP_USED_KB * 100 / SWAP_TOTAL_KB))
echo " 💿 Swap Usage: $(format_bytes $((SWAP_USED_KB * 1024))) / $(format_bytes $((SWAP_TOTAL_KB * 1024))) ($SWAP_PERCENT%)" >&2
else
echo " 💿 Swap: Not configured" >&2
fi
fi
else
report_memory "WARNING" "Unable to read /proc/meminfo - memory monitoring limited"
fi
;;
"Darwin")
# macOS memory information
if command -v vm_stat &> /dev/null; then
# Get page size
PAGE_SIZE=$(vm_stat | grep 'page size of' | awk '{print $8}' || echo "4096")
# Get memory statistics
VM_STAT_OUTPUT=$(vm_stat)
PAGES_FREE=$(echo "$VM_STAT_OUTPUT" | grep 'Pages free:' | awk '{print $3}' | tr -d '.')
PAGES_ACTIVE=$(echo "$VM_STAT_OUTPUT" | grep 'Pages active:' | awk '{print $3}' | tr -d '.')
PAGES_INACTIVE=$(echo "$VM_STAT_OUTPUT" | grep 'Pages inactive:' | awk '{print $3}' | tr -d '.')
PAGES_SPECULATIVE=$(echo "$VM_STAT_OUTPUT" | grep 'Pages speculative:' | awk '{print $3}' | tr -d '.' || echo "0")
PAGES_WIRED=$(echo "$VM_STAT_OUTPUT" | grep 'Pages wired down:' | awk '{print $4}' | tr -d '.')
# Calculate memory usage
TOTAL_PAGES=$((PAGES_FREE + PAGES_ACTIVE + PAGES_INACTIVE + PAGES_SPECULATIVE + PAGES_WIRED))
USED_PAGES=$((PAGES_ACTIVE + PAGES_INACTIVE + PAGES_SPECULATIVE + PAGES_WIRED))
TOTAL_MEMORY_KB=$((TOTAL_PAGES * PAGE_SIZE / 1024))
USED_MEMORY_KB=$((USED_PAGES * PAGE_SIZE / 1024))
MEMORY_PERCENT=$((USED_MEMORY_KB * 100 / TOTAL_MEMORY_KB))
echo " 🖥️ Total Memory: $(format_bytes $((TOTAL_MEMORY_KB * 1024)))" >&2
echo " 📈 Used Memory: $(format_bytes $((USED_MEMORY_KB * 1024))) ($MEMORY_PERCENT%)" >&2
# Check swap usage on macOS
if command -v sysctl &> /dev/null; then
SWAP_USAGE=$(sysctl vm.swapusage 2>/dev/null | grep -oE 'used = [0-9.]+[KMGT]?' | awk '{print $3}' || echo "0")
SWAP_TOTAL=$(sysctl vm.swapusage 2>/dev/null | grep -oE 'total = [0-9.]+[KMGT]?' | awk '{print $3}' || echo "0")
echo " 💿 Swap Usage: $SWAP_USAGE / $SWAP_TOTAL" >&2
fi
else
report_memory "WARNING" "vm_stat command not available - memory monitoring limited"
fi
;;
*)
report_memory "WARNING" "Unsupported operating system ($OS_TYPE) - limited memory monitoring"
;;
esac
# 2. Process-Specific Memory Analysis
echo "🔍 Analyzing development process memory usage..." >&2
# Define process patterns for different development environments
DEV_PROCESSES=("node" "python" "java" "ruby" "php" "go" "rust" "dotnet" "code" "claude")
TOTAL_DEV_MEMORY_KB=0
PROCESS_COUNT=0
for process in "${DEV_PROCESSES[@]}"; do
if command -v pgrep &> /dev/null; then
# Use pgrep for more accurate process detection
PIDS=$(pgrep -f "$process" 2>/dev/null || true)
if [ -n "$PIDS" ]; then
PROCESS_MEMORY=0
PROC_COUNT=0
# Calculate memory for all matching processes
for pid in $PIDS; do
if [ -f "/proc/$pid/status" ]; then
# Linux: Read from /proc/pid/status
PID_MEMORY=$(grep '^VmRSS:' "/proc/$pid/status" 2>/dev/null | awk '{print $2}' || echo "0")
elif command -v ps &> /dev/null; then
# macOS/Other: Use ps command
PID_MEMORY=$(ps -o rss= -p "$pid" 2>/dev/null | awk '{print $1}' || echo "0")
else
PID_MEMORY=0
fi
PROCESS_MEMORY=$((PROCESS_MEMORY + PID_MEMORY))
PROC_COUNT=$((PROC_COUNT + 1))
done
if [ "$PROCESS_MEMORY" -gt 0 ]; then
echo " 🔧 $process processes: $PROC_COUNT running, $(format_bytes $((PROCESS_MEMORY * 1024))) memory" >&2
TOTAL_DEV_MEMORY_KB=$((TOTAL_DEV_MEMORY_KB + PROCESS_MEMORY))
PROCESS_COUNT=$((PROCESS_COUNT + PROC_COUNT))
fi
fi
fi
done
if [ "$TOTAL_DEV_MEMORY_KB" -gt 0 ]; then
DEV_MEMORY_PERCENT=$((TOTAL_DEV_MEMORY_KB * 100 / TOTAL_MEMORY_KB))
echo " 📊 Total dev processes: $PROCESS_COUNT processes, $(format_bytes $((TOTAL_DEV_MEMORY_KB * 1024))) ($DEV_MEMORY_PERCENT% of system)" >&2
else
echo " 📋 No development processes detected" >&2
fi
# 3. Container Memory Monitoring (if applicable)
echo "🐳 Checking container memory usage..." >&2
if command -v docker &> /dev/null && docker info >/dev/null 2>&1; then
# Check Docker container memory usage
RUNNING_CONTAINERS=$(docker ps --format "table {{.Names}}" --no-trunc 2>/dev/null | tail -n +2 | wc -l || echo "0")
if [ "$RUNNING_CONTAINERS" -gt 0 ]; then
echo " 🐳 Docker containers: $RUNNING_CONTAINERS running" >&2
# Get container memory stats (basic)
CONTAINER_STATS=$(docker stats --no-stream --format "table {{.Container}}\t{{.MemUsage}}" 2>/dev/null | tail -n +2 | head -5 || true)
if [ -n "$CONTAINER_STATS" ]; then
echo " 📊 Top container memory usage:" >&2
echo "$CONTAINER_STATS" | while read line; do
echo " $line" >&2
done
fi
else
echo " 🐳 No running Docker containers" >&2
fi
else
echo " 🐳 Docker not available or not running" >&2
fi
# 4. Memory Threshold Analysis
echo "⚠️ Analyzing memory thresholds..." >&2
# Check system memory thresholds
if [ "$MEMORY_PERCENT" -ge "$CRITICAL_MEMORY_THRESHOLD" ]; then
report_memory "ERROR" "Critical system memory usage: $MEMORY_PERCENT% (threshold: $CRITICAL_MEMORY_THRESHOLD%)"
elif [ "$MEMORY_PERCENT" -ge "$HIGH_MEMORY_THRESHOLD" ]; then
report_memory "WARNING" "High system memory usage: $MEMORY_PERCENT% (threshold: $HIGH_MEMORY_THRESHOLD%)"
else
report_memory "PASS" "System memory usage within normal range: $MEMORY_PERCENT%"
fi
# Check swap usage
if [ "$SWAP_PERCENT" -ge "$HIGH_SWAP_THRESHOLD" ]; then
report_memory "WARNING" "High swap usage detected: $SWAP_PERCENT% (threshold: $HIGH_SWAP_THRESHOLD%)"
else
if [ "$SWAP_PERCENT" -gt 0 ]; then
report_memory "INFO" "Swap usage normal: $SWAP_PERCENT%"
fi
fi
# Check for potential memory leaks (high dev process usage)
if [ "$TOTAL_DEV_MEMORY_KB" -gt 0 ] && [ "$DEV_MEMORY_PERCENT" -ge 50 ]; then
report_memory "WARNING" "Development processes using significant memory: $DEV_MEMORY_PERCENT% of system"
elif [ "$TOTAL_DEV_MEMORY_KB" -gt 0 ]; then
report_memory "INFO" "Development process memory usage normal: $DEV_MEMORY_PERCENT% of system"
fi
# 5. Memory Recommendations
echo "💡 Memory optimization recommendations..." >&2
if [ "$MEMORY_PERCENT" -ge "$HIGH_MEMORY_THRESHOLD" ]; then
echo " • Consider closing unused applications and browser tabs" >&2
echo " • Restart development servers to free up memory" >&2
echo " • Check for memory leaks in your applications" >&2
fi
if [ "$SWAP_PERCENT" -ge "$HIGH_SWAP_THRESHOLD" ]; then
echo " • High swap usage may slow down development" >&2
echo " • Consider adding more RAM or closing applications" >&2
fi
if [ "$PROCESS_COUNT" -gt 10 ]; then
echo " • Many development processes running ($PROCESS_COUNT)" >&2
echo " • Consider stopping unused development servers" >&2
fi
# 6. Generate Memory Summary
echo "" >&2
echo "📋 Memory Usage Summary:" >&2
echo "========================" >&2
echo " 💾 System Memory: $MEMORY_PERCENT% used ($(format_bytes $((USED_MEMORY_KB * 1024))) / $(format_bytes $((TOTAL_MEMORY_KB * 1024))))" >&2
[ "$SWAP_PERCENT" -gt 0 ] && echo " 💿 Swap Usage: $SWAP_PERCENT%" >&2
echo " 🔧 Dev Processes: $PROCESS_COUNT running ($(format_bytes $((TOTAL_DEV_MEMORY_KB * 1024))))" >&2
echo " ⚠️ Warnings: $WARNINGS" >&2
echo " ❌ Critical Issues: $ERRORS" >&2
if [ "$ERRORS" -eq 0 ] && [ "$WARNINGS" -eq 0 ]; then
echo " 🎉 Status: OPTIMAL - Memory usage is healthy" >&2
elif [ "$ERRORS" -eq 0 ]; then
echo " ✅ Status: GOOD - Memory usage acceptable with minor warnings" >&2
else
echo " ❌ Status: CRITICAL - Memory usage requires immediate attention" >&2
fi
echo "" >&2
echo "💡 Memory Best Practices:" >&2
echo " • Monitor memory usage regularly during development" >&2
echo " • Use memory profiling tools to identify leaks" >&2
echo " • Restart development servers periodically" >&2
echo " • Consider using lighter development tools when memory is limited" >&2
echo " • Close unused applications and browser tabs" >&2
# Memory monitoring complete
exit 0Memory percentage calculation shows values over 100% or negative
MemAvailable fallback calculation adds cached memory twice. For older Linux: 'USED_MEMORY_KB=$((TOTAL_MEMORY_KB - FREE_MEMORY_KB))' excluding buffers/cache. Or upgrade kernel to 3.14+ with MemAvailable support.
macOS vm_stat parsing fails showing zero memory usage
PAGE_SIZE extraction fails when format changes. Hardcode: 'PAGE_SIZE=4096' (Intel) or 'PAGE_SIZE=16384' (Apple Silicon M1/M2). Or use: 'sysctl hw.pagesize | awk '{print $2}''.
pgrep returns processes from other users causing inflated dev memory
pgrep matches all users. Restrict: 'pgrep -u $USER -f "$process"' showing only current user's processes. Or filter: 'ps -u $USER -o pid,comm,rss' for ownership validation.
Docker stats command hangs indefinitely when containers unhealthy
docker stats --no-stream blocks on slow containers. Add timeout: 'timeout 5 docker stats --no-stream' or skip: 'docker stats --no-stream --format json 2>/dev/null | head -n 1' limiting output.
Hook execution time exceeds timeout (5000ms) on large systems
Process enumeration slow with 100+ dev processes. Increase timeout: '"timeout": 10000' in hookConfig. Or limit: 'pgrep -f "$process" | head -20' checking only top processes.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters