Server Memory Optimization: A Practical Guide
Learn how to optimize memory usage on your servers. Cache management, swap tuning, and identifying memory leaks.
> For people who run servers
Understanding Server Memory
Memory management is one of the most important aspects of server performance. Poor memory utilization leads to swapping, which can make even a powerful server crawl.
Diagnosing Memory Issues
Check Current Usage
# Overview
free -h
# Detailed per-process
ps aux --sort=-%mem | head -20
# Real-time monitoring
htopUnderstanding the Output
- Used: Memory actively in use by applications
- Buff/Cache: Memory used for disk caching (can be reclaimed)
- Available: Memory that can be given to new applications (includes reclaimable cache)
- Swap: Disk-based memory — if heavily used, you need more RAM
Common Memory Hogs
1. Database Servers
MySQL and PostgreSQL can consume massive amounts of memory. Tune buffer pools:
# MySQL - /etc/mysql/mysql.conf.d/mysqld.cnf
innodb_buffer_pool_size = 1G # 50-70% of available RAM for dedicated DB servers
key_buffer_size = 256M
query_cache_size = 64M2. Web Servers
Nginx and Apache have different memory profiles:
# Nginx - Much more memory efficient
worker_processes auto;
worker_connections 1024;3. Application Memory Leaks
Node.js, Python, and Java applications can leak memory over time:
# Monitor a specific process over time
pidstat -r -p $(pgrep node) 5Optimization Techniques
Tune Swap Behavior
# Check current swappiness (default: 60)
cat /proc/sys/vm/swappiness
# Lower for servers with enough RAM (10 is good for most servers)
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -pClear System Cache
# Drop page cache, dentries, and inodes
sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
# ServerGuard can do this with one click from the dashboardEnable Transparent Huge Pages (for databases)
echo 'madvise' | sudo tee /sys/kernel/mm/transparent_hugepage/enabledLimit Per-Process Memory
# Using systemd
[Service]
MemoryLimit=512M
MemoryMax=1GMemory Boost with ServerGuard
ServerGuard's Memory Boost feature performs safe optimization in one click:
- Clears filesystem cache (when safe)
- Drops unused kernel objects
- Syncs dirty pages to disk first
- Reports memory freed
This is available in the System Controls section of your dashboard.
Monitoring Best Practices
- Set alerts at 85% — Gives you time to investigate before it becomes critical
- Track swap usage — Any consistent swap use means you're low on RAM
- Monitor OOM killer — Check
dmesgfor out-of-memory events - Trend analysis — Gradual increase over days/weeks indicates a memory leak
- Per-process tracking — Identify which applications grow over time
Conclusion
Memory optimization is a balance between giving applications enough room to perform and not wasting resources. Start by monitoring, identify the biggest consumers, and tune accordingly. ServerGuard makes this easy with real-time memory monitoring, one-click optimization, and trend analysis.