Skip to content
Statusthreat center ›
← All guides
Performance · 10 min · 2025-05-28

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

Written in 2025 for ServerGuard
Product statements in this guide may be out of date. For what works today, see the ServerGuard page and the capabilities.

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
htop

Understanding 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 = 64M

2. 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) 5

Optimization 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 -p

Clear 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 dashboard

Enable Transparent Huge Pages (for databases)

echo 'madvise' | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

Limit Per-Process Memory

# Using systemd
[Service]
MemoryLimit=512M
MemoryMax=1G

Memory Boost with ServerGuard

ServerGuard's Memory Boost feature performs safe optimization in one click:

  1. Clears filesystem cache (when safe)
  2. Drops unused kernel objects
  3. Syncs dirty pages to disk first
  4. Reports memory freed

This is available in the System Controls section of your dashboard.

Monitoring Best Practices

  1. Set alerts at 85% — Gives you time to investigate before it becomes critical
  2. Track swap usage — Any consistent swap use means you're low on RAM
  3. Monitor OOM killer — Check dmesg for out-of-memory events
  4. Trend analysis — Gradual increase over days/weeks indicates a memory leak
  5. 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.