๐ง Optimizing Memory Usage: Simple Guide
Letโs optimize memory usage on your Alpine Linux system! ๐พ This guide uses easy steps and simple words. Weโll make your RAM work efficiently and boost system performance! ๐
๐ค What is Memory Optimization?
Memory optimization is like organizing your desk so you can find and use everything efficiently!
Think of memory optimization like:
- ๐ A smart filing system that keeps frequently used documents within armโs reach
- ๐ง A kitchen where commonly used tools are stored in the most accessible spots
- ๐ก A library where popular books are placed on easy-to-reach shelves
๐ฏ What You Need
Before we start, you need:
- โ Alpine Linux system running
- โ Root access or sudo permissions
- โ Basic understanding of system resources
- โ Monitoring tools to measure memory usage
๐ Step 1: Analyze Current Memory Usage
Check Memory Information and Usage
First, letโs see how your system is currently using memory! ๐
What weโre doing: Gathering detailed information about your memory usage patterns to identify optimization opportunities.
# Update package lists and install memory analysis tools
apk update
apk add htop sysstat procps util-linux
# Check basic memory information
free -h
cat /proc/meminfo | head -20
# Check detailed memory usage
cat /proc/meminfo | grep -E "(MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree)"
# Monitor memory usage over time
vmstat 1 5
# Check memory usage by process
ps aux --sort=-%mem | head -10
# Check which processes are using the most memory
top -o %MEM -n 1 | head -15
# Monitor system memory statistics
sar -r 1 5
# Check for memory leaks or unusual patterns
cat /proc/buddyinfo
cat /proc/slabinfo | head -20
# Check swap usage
swapon --show
cat /proc/swaps
# Check memory zones and NUMA information
cat /proc/zoneinfo | grep -E "(Node|zone|present|managed|protection)"
What this does: ๐ Gives you a complete picture of how your system is using memory.
Example output:
total used free shared buff/cache available
Mem: 2.0Gi 456Mi 1.2Gi 12Mi 356Mi 1.4Gi
Swap: 1.0Gi 0B 1.0Gi
MemTotal: 2097152 kB
MemFree: 1228800 kB
MemAvailable: 1474560 kB
Buffers: 45632 kB
Cached: 298764 kB
SwapTotal: 1048576 kB
SwapFree: 1048576 kB
PID USER %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
1234 root 2.1 8.5 245760 178432 ? Ss 14:30 0:15 /usr/bin/python3
5678 www-data 0.8 5.2 145280 108544 ? S 14:32 0:08 nginx: worker
What this means: You now have baseline memory usage data to work with! โ
๐ก Important Tips
Tip: Monitor memory usage during different workloads to understand patterns! ๐ก
Warning: Aggressive memory optimization can impact system stability! โ ๏ธ
๐ ๏ธ Step 2: Configure Memory Management Settings
Optimize Kernel Memory Parameters
Now letโs configure your system for efficient memory usage! ๐
What weโre doing: Adjusting kernel parameters that control how Linux manages memory allocation, caching, and swapping.
# Check current memory management settings
cat /proc/sys/vm/swappiness
cat /proc/sys/vm/dirty_ratio
cat /proc/sys/vm/dirty_background_ratio
cat /proc/sys/vm/vfs_cache_pressure
# Create optimized memory management configuration
cat > /etc/sysctl.d/99-memory-optimization.conf << 'EOF'
# Memory Optimization Settings
# Swappiness (0-100): Lower values prefer RAM over swap
# 10 = use swap only when necessary
vm.swappiness = 10
# Dirty page management
# dirty_ratio: % of total memory for dirty pages before blocking writes
vm.dirty_ratio = 20
# dirty_background_ratio: % when background writing starts
vm.dirty_background_ratio = 5
# Cache pressure (default 100)
# Lower values keep more cache, higher values free cache sooner
vm.vfs_cache_pressure = 50
# Overcommit handling
# 0 = heuristic overcommit, 1 = always overcommit, 2 = never overcommit
vm.overcommit_memory = 0
vm.overcommit_ratio = 50
# Memory compaction
vm.compact_memory = 1
# Page allocation behavior
vm.min_free_kbytes = 65536
# Transparent Hugepages
# Performance boost for large memory applications
vm.nr_hugepages = 128
# Memory reclaim behavior
vm.zone_reclaim_mode = 0
# OOM killer behavior
vm.panic_on_oom = 0
vm.oom_kill_allocating_task = 1
# Kernel memory management
kernel.shmmax = 268435456
kernel.shmall = 2097152
# Network memory optimization
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 5000
EOF
# Apply the memory optimization settings
sysctl -p /etc/sysctl.d/99-memory-optimization.conf
# Verify the changes
echo "=== Memory Settings Applied ==="
sysctl vm.swappiness vm.dirty_ratio vm.vfs_cache_pressure
# Configure hugepages for better memory performance
if [ -f /sys/kernel/mm/transparent_hugepage/enabled ]; then
echo "always" > /sys/kernel/mm/transparent_hugepage/enabled
echo "Transparent hugepages enabled"
else
echo "Transparent hugepages not supported"
fi
# Create memory monitoring script
cat > /usr/local/bin/memory-monitor.sh << 'EOF'
#!/bin/bash
# Memory Usage Monitor
while true; do
clear
echo "๐ง MEMORY USAGE MONITOR - $(date)"
echo "==============================="
echo
echo "๐ Memory Overview:"
free -h | grep -E "(Mem|Swap)"
echo
echo "๐พ Memory Distribution:"
awk '/MemTotal/{total=$2}
/MemFree/{free=$2}
/MemAvailable/{avail=$2}
/Buffers/{buffers=$2}
/^Cached/{cached=$2}
/SwapTotal/{stotal=$2}
/SwapFree/{sfree=$2}
END {
used=total-free;
printf " Total: %8.1f MB\n", total/1024;
printf " Used: %8.1f MB (%.1f%%)\n", used/1024, used*100/total;
printf " Free: %8.1f MB (%.1f%%)\n", free/1024, free*100/total;
printf " Available: %8.1f MB (%.1f%%)\n", avail/1024, avail*100/total;
printf " Buffers: %8.1f MB\n", buffers/1024;
printf " Cached: %8.1f MB\n", cached/1024;
printf " Swap Used: %8.1f MB\n", (stotal-sfree)/1024;
}' /proc/meminfo
echo
echo "๐ฅ Top Memory Consumers:"
ps aux --sort=-%mem | awk 'NR<=6 {printf " %-12s %6s %s\n", $1, $4"%", $11}'
echo
echo "๐ Memory Statistics:"
echo " Swappiness: $(cat /proc/sys/vm/swappiness)"
echo " Cache Pressure: $(cat /proc/sys/vm/vfs_cache_pressure)"
echo " Dirty Ratio: $(cat /proc/sys/vm/dirty_ratio)%"
echo
if [ -f /sys/kernel/mm/transparent_hugepage/enabled ]; then
echo "๐ง Hugepages:"
echo " Status: $(cat /sys/kernel/mm/transparent_hugepage/enabled | grep -o '\[.*\]' | tr -d '[]')"
if [ -f /proc/meminfo ]; then
grep -E "HugePages|Hugepagesize" /proc/meminfo | awk '{printf " %s: %s %s\n", $1, $2, $3}'
fi
echo
fi
echo "Press Ctrl+C to exit, refreshing in 3 seconds..."
sleep 3
done
EOF
chmod +x /usr/local/bin/memory-monitor.sh
Memory optimization settings applied:
- Reduced swappiness for better RAM utilization
- Optimized cache and buffer management
- Configured page allocation for stability
- Enabled transparent hugepages for performance
What this means: Your memory management is now optimized for better performance! ๐
๐ฎ Step 3: Optimize Application Memory Usage
Manage Process Memory Consumption
Letโs optimize how applications use memory! ๐ฏ
What weโre doing: Configuring applications and services to use memory more efficiently through limits, optimization, and better resource management.
# Install memory limit tools
apk add cgroup-tools
# Create memory limits for processes
create_memory_limits() {
# Enable cgroups memory controller
mount -t cgroup -o memory cgroup /sys/fs/cgroup/memory 2>/dev/null || echo "cgroups already mounted"
# Create memory groups for different service types
mkdir -p /sys/fs/cgroup/memory/webservices
mkdir -p /sys/fs/cgroup/memory/databases
mkdir -p /sys/fs/cgroup/memory/background
# Set memory limits (in bytes)
echo 512M > /sys/fs/cgroup/memory/webservices/memory.limit_in_bytes
echo 1G > /sys/fs/cgroup/memory/databases/memory.limit_in_bytes
echo 256M > /sys/fs/cgroup/memory/background/memory.limit_in_bytes
echo "Memory cgroups configured"
}
create_memory_limits
# Optimize specific applications
optimize_nginx_memory() {
if command -v nginx >/dev/null; then
cat > /etc/nginx/conf.d/memory-optimization.conf << 'EOF'
# Nginx Memory Optimization
# Worker process optimization
worker_processes auto;
worker_rlimit_nofile 8192;
# Connection optimization
worker_connections 1024;
# Buffer sizes (reduce memory per connection)
client_body_buffer_size 8k;
client_header_buffer_size 1k;
large_client_header_buffers 2 1k;
client_max_body_size 8m;
# Compression to reduce memory usage
gzip on;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_vary on;
# Connection pooling
upstream_keepalive_connections 32;
keepalive_timeout 30s;
keepalive_requests 100;
EOF
echo "Nginx memory optimization configured"
fi
}
optimize_nginx_memory
# Create application memory monitor
cat > /usr/local/bin/app-memory-monitor.sh << 'EOF'
#!/bin/bash
# Application Memory Monitor
LOG_FILE="/var/log/app-memory.log"
monitor_application() {
local app_name="$1"
local memory_limit_mb="$2"
# Find processes for the application
for pid in $(pgrep "$app_name"); do
# Get memory usage in MB
local memory_mb=$(ps -p $pid -o rss= | awk '{print $1/1024}')
local memory_pct=$(ps -p $pid -o %mem= | awk '{print $1}')
# Log current usage
echo "$(date '+%Y-%m-%d %H:%M:%S') - $app_name (PID:$pid): ${memory_mb}MB (${memory_pct}%)" >> $LOG_FILE
# Check if exceeding limit
if (( $(echo "$memory_mb > $memory_limit_mb" | bc -l) )); then
echo "$(date '+%Y-%m-%d %H:%M:%S') - WARNING: $app_name (PID:$pid) exceeding memory limit: ${memory_mb}MB > ${memory_limit_mb}MB" >> $LOG_FILE
# Optional: Restart the application if memory usage is too high
# systemctl restart $app_name
fi
done
}
# Monitor common applications
monitor_application "nginx" 100
monitor_application "mysql" 500
monitor_application "python" 200
# Clean old log entries (keep last 1000 lines)
tail -1000 $LOG_FILE > ${LOG_FILE}.tmp && mv ${LOG_FILE}.tmp $LOG_FILE
EOF
chmod +x /usr/local/bin/app-memory-monitor.sh
# Set up automatic memory monitoring
echo "*/5 * * * * /usr/local/bin/app-memory-monitor.sh" >> /etc/crontabs/root
# Create memory cleanup script
cat > /usr/local/bin/memory-cleanup.sh << 'EOF'
#!/bin/bash
# Memory Cleanup and Optimization
echo "๐งน Starting memory cleanup at $(date)"
# Clear page cache, dentries, and inodes
echo "Clearing filesystem caches..."
sync
echo 3 > /proc/sys/vm/drop_caches
# Compact memory to reduce fragmentation
echo "Compacting memory..."
echo 1 > /proc/sys/vm/compact_memory 2>/dev/null || echo "Memory compaction not available"
# Clear swap if usage is low
swap_usage=$(free | grep Swap | awk '{print $3}')
if [ "$swap_usage" -lt 50000 ]; then # Less than ~50MB
echo "Clearing swap..."
swapoff -a && swapon -a
fi
# Merge identical pages
if [ -f /sys/kernel/mm/ksm/run ]; then
echo "Activating kernel samepage merging..."
echo 1 > /sys/kernel/mm/ksm/run
fi
# Log memory status after cleanup
echo "Memory status after cleanup:"
free -h
echo "โ
Memory cleanup completed at $(date)"
EOF
chmod +x /usr/local/bin/memory-cleanup.sh
# Schedule automatic memory cleanup
echo "0 3 * * * /usr/local/bin/memory-cleanup.sh >> /var/log/memory-cleanup.log 2>&1" >> /etc/crontabs/root
Application optimizations applied:
- Memory limits set for different service categories
- Nginx configured for memory efficiency
- Automatic monitoring for memory-hungry applications
- Scheduled cleanup to prevent memory fragmentation
What this creates:
Memory limits: Applied per application category
Process monitoring: Continuous tracking with alerts
Cache management: Automatic cleanup and optimization
Resource control: cgroups for memory allocation
Great job! Application memory usage is optimized! ๐
๐ Step 4: Advanced Memory Optimizations
Fine-tune Memory Allocation
Now letโs implement advanced memory optimization techniques! ๐
What weโre doing: Configuring advanced memory features like NUMA optimization, memory balancing, and specialized allocation strategies.
# Check NUMA topology
check_numa_support() {
if command -v numactl >/dev/null 2>&1; then
echo "NUMA topology:"
numactl --hardware
# Optimize NUMA settings
echo 1 > /proc/sys/kernel/numa_balancing
echo "NUMA balancing enabled"
else
echo "NUMA not available or numactl not installed"
apk add numactl 2>/dev/null || echo "numactl package not available"
fi
}
check_numa_support
# Configure memory zones optimization
optimize_memory_zones() {
echo "Optimizing memory zones..."
# Prevent memory reclaim from remote nodes
echo 0 > /proc/sys/vm/zone_reclaim_mode
# Optimize dirty page writeback
echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
echo 3000 > /proc/sys/vm/dirty_expire_centisecs
# Configure memory allocation behavior
echo 1 > /proc/sys/vm/oom_dump_tasks
echo 0 > /proc/sys/vm/panic_on_oom
echo "Memory zones optimized"
}
optimize_memory_zones
# Create memory pressure monitoring
cat > /usr/local/bin/memory-pressure-monitor.sh << 'EOF'
#!/bin/bash
# Memory Pressure Detection and Response
PRESSURE_LOG="/var/log/memory-pressure.log"
ALERT_THRESHOLD=85 # Alert when memory usage exceeds 85%
check_memory_pressure() {
# Get memory usage percentage
local mem_usage=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
local swap_usage=$(free | grep Swap | awk '{if($2>0) printf "%.0f", $3/$2 * 100; else print 0}')
echo "$(date '+%Y-%m-%d %H:%M:%S'): Memory: ${mem_usage}%, Swap: ${swap_usage}%" >> $PRESSURE_LOG
# Check for memory pressure
if [ "$mem_usage" -gt "$ALERT_THRESHOLD" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S'): HIGH MEMORY PRESSURE DETECTED: ${mem_usage}%" >> $PRESSURE_LOG
# Take corrective actions
perform_emergency_cleanup
# Send alert
logger -p local0.alert "High memory pressure detected: ${mem_usage}% memory usage"
fi
# Check for swap thrashing
if [ "$swap_usage" -gt 50 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S'): SWAP THRASHING DETECTED: ${swap_usage}% swap usage" >> $PRESSURE_LOG
logger -p local0.alert "Swap thrashing detected: ${swap_usage}% swap usage"
fi
}
perform_emergency_cleanup() {
echo "$(date '+%Y-%m-%d %H:%M:%S'): Performing emergency memory cleanup" >> $PRESSURE_LOG
# Clear caches
sync
echo 3 > /proc/sys/vm/drop_caches
# Kill high-memory processes if critical
# (Uncomment and customize as needed)
# pkill -f "memory-hungry-process"
# Compact memory
echo 1 > /proc/sys/vm/compact_memory 2>/dev/null
echo "$(date '+%Y-%m-%d %H:%M:%S'): Emergency cleanup completed" >> $PRESSURE_LOG
}
# Run memory pressure check
check_memory_pressure
# If run with "daemon" argument, run continuously
if [ "$1" = "daemon" ]; then
while true; do
sleep 60
check_memory_pressure
done
fi
EOF
chmod +x /usr/local/bin/memory-pressure-monitor.sh
# Start memory pressure monitoring
/usr/local/bin/memory-pressure-monitor.sh daemon &
# Configure memory optimization for databases
optimize_database_memory() {
echo "Configuring database memory optimization..."
# MySQL/MariaDB optimization
if command -v mysql >/dev/null; then
cat > /etc/mysql/conf.d/memory-optimization.cnf << 'EOF'
[mysqld]
# Memory optimization for MySQL/MariaDB
# Buffer pool (70-80% of available memory for dedicated DB server)
innodb_buffer_pool_size = 512M
innodb_buffer_pool_instances = 4
# Query cache
query_cache_size = 64M
query_cache_type = 1
# Connection and thread settings
max_connections = 100
thread_cache_size = 16
# Table cache
table_open_cache = 2000
table_definition_cache = 1400
# Sort and join buffers
sort_buffer_size = 2M
join_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
# Temporary tables
tmp_table_size = 64M
max_heap_table_size = 64M
# Log buffer
innodb_log_buffer_size = 16M
EOF
echo "MySQL memory optimization configured"
fi
# PostgreSQL optimization
if command -v postgres >/dev/null; then
cat >> /etc/postgresql/postgresql.conf << 'EOF'
# Memory optimization for PostgreSQL
shared_buffers = 256MB
effective_cache_size = 1GB
work_mem = 4MB
maintenance_work_mem = 64MB
checkpoint_completion_target = 0.7
wal_buffers = 16MB
default_statistics_target = 100
EOF
echo "PostgreSQL memory optimization configured"
fi
}
optimize_database_memory
# Create memory usage analyzer
cat > /usr/local/bin/memory-analyzer.sh << 'EOF'
#!/bin/bash
# Memory Usage Analysis Tool
REPORT_FILE="/var/log/memory-analysis-$(date +%Y%m%d-%H%M%S).txt"
generate_memory_report() {
echo "๐ง MEMORY USAGE ANALYSIS REPORT - $(date)" > $REPORT_FILE
echo "=============================================" >> $REPORT_FILE
echo >> $REPORT_FILE
echo "๐ System Memory Overview:" >> $REPORT_FILE
free -h >> $REPORT_FILE
echo >> $REPORT_FILE
echo "๐ Memory Distribution:" >> $REPORT_FILE
cat /proc/meminfo | grep -E "(MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree|Dirty|Writeback|Slab)" >> $REPORT_FILE
echo >> $REPORT_FILE
echo "๐ฅ Top 10 Memory Consuming Processes:" >> $REPORT_FILE
ps aux --sort=-%mem | head -11 | awk '{printf "%-10s %6s %6s %s\n", $1, $4, $6, $11}' >> $REPORT_FILE
echo >> $REPORT_FILE
echo "๐พ Memory Usage by User:" >> $REPORT_FILE
ps aux | awk 'NR>1 {mem[$1]+=$6} END {for(user in mem) printf "%-10s %8.1f MB\n", user, mem[user]/1024}' | sort -k2 -nr >> $REPORT_FILE
echo >> $REPORT_FILE
echo "๐๏ธ Slab Cache Usage (Top 10):" >> $REPORT_FILE
cat /proc/slabinfo | sort -k3 -nr | head -11 | awk '{printf "%-30s %8s %8s\n", $1, $2, $3}' >> $REPORT_FILE
echo >> $REPORT_FILE
echo "๐ง Current Memory Settings:" >> $REPORT_FILE
echo "Swappiness: $(cat /proc/sys/vm/swappiness)" >> $REPORT_FILE
echo "VFS Cache Pressure: $(cat /proc/sys/vm/vfs_cache_pressure)" >> $REPORT_FILE
echo "Dirty Ratio: $(cat /proc/sys/vm/dirty_ratio)" >> $REPORT_FILE
echo "Overcommit Memory: $(cat /proc/sys/vm/overcommit_memory)" >> $REPORT_FILE
echo >> $REPORT_FILE
if [ -f /sys/kernel/mm/transparent_hugepage/enabled ]; then
echo "๐ง Hugepage Information:" >> $REPORT_FILE
grep -E "HugePages|Hugepagesize" /proc/meminfo >> $REPORT_FILE
echo "THP Status: $(cat /sys/kernel/mm/transparent_hugepage/enabled)" >> $REPORT_FILE
echo >> $REPORT_FILE
fi
echo "๐ Memory Recommendations:" >> $REPORT_FILE
local mem_usage=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
if [ "$mem_usage" -gt 80 ]; then
echo "โ ๏ธ High memory usage detected (${mem_usage}%)" >> $REPORT_FILE
echo " - Consider adding more RAM" >> $REPORT_FILE
echo " - Review memory-intensive applications" >> $REPORT_FILE
echo " - Implement more aggressive caching policies" >> $REPORT_FILE
elif [ "$mem_usage" -lt 50 ]; then
echo "โ
Good memory utilization (${mem_usage}%)" >> $REPORT_FILE
echo " - Consider increasing cache sizes for better performance" >> $REPORT_FILE
echo " - Memory optimization is working well" >> $REPORT_FILE
fi
echo >> $REPORT_FILE
echo "Report generated at: $(date)" >> $REPORT_FILE
echo "โ
Analysis complete. Report saved to: $REPORT_FILE"
}
generate_memory_report
EOF
chmod +x /usr/local/bin/memory-analyzer.sh
# Run initial memory analysis
/usr/local/bin/memory-analyzer.sh
echo "Advanced memory optimizations configured!"
Advanced optimizations applied:
- NUMA balancing for multi-socket systems
- Memory pressure monitoring with automatic response
- Database-specific memory tuning
- Comprehensive memory analysis and reporting
What this does: Implements enterprise-level memory optimization strategies! ๐
Example: Memory Performance Testing ๐ก
What weโre doing: Running comprehensive memory stress tests to validate optimization effectiveness.
# Install memory testing tools
apk add stress-ng memtester
# Create memory benchmark script
cat > /usr/local/bin/memory-benchmark.sh << 'EOF'
#!/bin/bash
# Memory Performance Benchmark Suite
RESULTS_FILE="/var/log/memory-benchmark-$(date +%Y%m%d-%H%M%S).log"
echo "๐ง MEMORY BENCHMARK SUITE - $(date)" | tee $RESULTS_FILE
echo "===================================" | tee -a $RESULTS_FILE
echo | tee -a $RESULTS_FILE
# System information
echo "๐พ System Memory Information:" | tee -a $RESULTS_FILE
free -h | tee -a $RESULTS_FILE
echo | tee -a $RESULTS_FILE
echo "โ๏ธ Memory Settings:" | tee -a $RESULTS_FILE
echo "Swappiness: $(cat /proc/sys/vm/swappiness)" | tee -a $RESULTS_FILE
echo "Cache Pressure: $(cat /proc/sys/vm/vfs_cache_pressure)" | tee -a $RESULTS_FILE
echo "Dirty Ratio: $(cat /proc/sys/vm/dirty_ratio)" | tee -a $RESULTS_FILE
echo | tee -a $RESULTS_FILE
# Memory allocation test
echo "๐ Memory Allocation Test:" | tee -a $RESULTS_FILE
start_time=$(date +%s)
stress-ng --vm 1 --vm-bytes 256M --timeout 30s --metrics-brief 2>&1 | grep "bogo ops" | tee -a $RESULTS_FILE
end_time=$(date +%s)
echo "Allocation test duration: $((end_time - start_time)) seconds" | tee -a $RESULTS_FILE
echo | tee -a $RESULTS_FILE
# Memory bandwidth test
echo "๐ Memory Bandwidth Test:" | tee -a $RESULTS_FILE
if command -v sysbench >/dev/null; then
sysbench memory --threads=1 --time=30 --memory-total-size=1G run 2>/dev/null | grep "transferred" | tee -a $RESULTS_FILE
else
echo "sysbench not available for bandwidth testing" | tee -a $RESULTS_FILE
fi
echo | tee -a $RESULTS_FILE
# Cache performance test
echo "๐๏ธ Cache Performance Test:" | tee -a $RESULTS_FILE
stress-ng --cache 1 --timeout 20s --metrics-brief 2>&1 | grep "bogo ops" | tee -a $RESULTS_FILE
echo | tee -a $RESULTS_FILE
# Memory pressure test
echo "โก Memory Pressure Test:" | tee -a $RESULTS_FILE
echo "Before pressure test:" | tee -a $RESULTS_FILE
free -h | grep Mem | tee -a $RESULTS_FILE
# Allocate 80% of available memory
available_mb=$(free -m | grep Mem | awk '{print $7}')
test_mb=$((available_mb * 80 / 100))
stress-ng --vm 1 --vm-bytes ${test_mb}M --timeout 10s --quiet &
stress_pid=$!
sleep 5
echo "During pressure test:" | tee -a $RESULTS_FILE
free -h | grep Mem | tee -a $RESULTS_FILE
wait $stress_pid
echo "After pressure test:" | tee -a $RESULTS_FILE
free -h | grep Mem | tee -a $RESULTS_FILE
echo | tee -a $RESULTS_FILE
# Swap performance test
echo "๐ฟ Swap Performance Test:" | tee -a $RESULTS_FILE
if [ "$(free | grep Swap | awk '{print $2}')" -gt 0 ]; then
echo "Testing swap performance..." | tee -a $RESULTS_FILE
# Force some swap usage
stress-ng --vm 1 --vm-bytes $(($(free -m | grep Mem | awk '{print $2}') + 100))M --timeout 10s --quiet &
stress_pid=$!
sleep 5
echo "Swap usage during test:" | tee -a $RESULTS_FILE
free -h | grep Swap | tee -a $RESULTS_FILE
wait $stress_pid
else
echo "No swap configured" | tee -a $RESULTS_FILE
fi
echo | tee -a $RESULTS_FILE
echo "โ
Memory benchmark completed. Results saved to: $RESULTS_FILE" | tee -a $RESULTS_FILE
EOF
chmod +x /usr/local/bin/memory-benchmark.sh
# Run memory benchmark
echo "Running memory performance benchmark..."
/usr/local/bin/memory-benchmark.sh
What this does: Provides comprehensive memory performance testing to validate your optimizations! ๐
๐จ Fix Common Problems
Problem 1: System running out of memory โ
What happened: Applications are consuming more memory than available. How to fix it: Implement emergency memory management!
# Check memory hogs
ps aux --sort=-%mem | head -10
# Kill memory-intensive processes
pkill -f "high-memory-process-name"
# Clear caches immediately
sync
echo 3 > /proc/sys/vm/drop_caches
# Increase swap if needed
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
Problem 2: System using too much swap โ
What happened: Excessive swapping is slowing down the system. How to fix it: Adjust swappiness and add more RAM!
# Reduce swappiness temporarily
echo 1 > /proc/sys/vm/swappiness
# Clear swap usage
swapoff -a && swapon -a
# Check what's causing swap usage
grep VmSwap /proc/*/status | sort -k2 -nr | head -10
Problem 3: Memory fragmentation issues โ
What happened: Available memory is fragmented and cannot be allocated. How to fix it: Compact memory and enable defragmentation!
# Force memory compaction
echo 1 > /proc/sys/vm/compact_memory
# Enable automatic compaction
echo 1 > /proc/sys/vm/compact_unevictable_allowed
# Check memory fragmentation
cat /proc/buddyinfo
# Enable transparent hugepages
echo always > /sys/kernel/mm/transparent_hugepage/enabled
Donโt worry! These problems happen to everyone. Youโre doing great! ๐ช
๐ก Simple Tips
- Monitor regularly ๐ - Track memory usage patterns over time
- Set reasonable limits ๐ฑ - Donโt over-optimize memory settings
- Test after changes ๐ค - Verify system stability after optimization
- Plan for growth ๐ช - Leave headroom for future memory needs
โ Check Everything Works
Letโs make sure everything is working:
# Check memory usage
free -h
# Verify memory settings
sysctl vm.swappiness vm.dirty_ratio vm.vfs_cache_pressure
# Check for memory pressure
cat /var/log/memory-pressure.log | tail -5
# Test memory allocation
stress-ng --vm 1 --vm-bytes 100M --timeout 5s --quiet && echo "Memory allocation test passed"
# Check running processes memory usage
ps aux --sort=-%mem | head -5
# Monitor system health
uptime
/usr/local/bin/memory-monitor.sh &
sleep 5
pkill -f memory-monitor.sh
# Run memory analysis
/usr/local/bin/memory-analyzer.sh
# You should see this
echo "Memory optimization is complete and working efficiently! โ
"
Good output:
total used free shared buff/cache available
Mem: 2.0Gi 456Mi 1.4Gi 12Mi 156Mi 1.5Gi
Swap: 1.0Gi 0B 1.0Gi
vm.swappiness = 10
vm.dirty_ratio = 20
vm.vfs_cache_pressure = 50
2025-06-03 18:45:32: Memory: 23%, Swap: 0%
Memory allocation test passed
USER %MEM RSS COMMAND
root 3.2 65432 /usr/bin/python3
www-data 2.1 43210 nginx: worker
mysql 1.8 36864 /usr/sbin/mysqld
load average: 0.15, 0.20, 0.18
โ
Success! Memory is optimized and system is running efficiently.
๐ What You Learned
Great job! Now you can:
- โ Analyze and monitor memory usage patterns effectively
- โ Configure kernel memory management parameters optimally
- โ Implement application-specific memory optimizations
- โ Set up memory pressure monitoring and automatic responses
- โ Use advanced memory features like hugepages and NUMA balancing
๐ฏ Whatโs Next?
Now you can try:
- ๐ Implementing memory profiling for application optimization
- ๐ ๏ธ Setting up memory-based auto-scaling for containers
- ๐ค Creating memory usage prediction and capacity planning
- ๐ Building custom memory management policies for specific workloads!
Remember: Every expert was once a beginner. Youโre doing amazing! ๐
Keep practicing and youโll become a memory optimization expert too! ๐ซ