Memory Leak Detection and Resolution
Detect and resolve memory leaks on Linux servers, identify which process is consuming RAM, and apply short and long-term fixes.
Table of Contents
Introduction
A memory leak occurs when an application fails to properly release memory it has used, causing RAM usage to continuously increase over time. Eventually the system runs out of memory, swap space fills up, and the server slows down or crashes. This guide covers how to detect and resolve memory leaks.
Symptoms
Typical signs of a memory leak:
- RAM usage continuously increases over time (returns to normal after restart)
- Available memory in
free -houtput gradually decreases - Swap usage increases
- Application slows down over time
- OOM (Out of Memory) killer activates and kills processes
oom-killmessages appear indmesg
Step 1: Check Current Memory Status
# General memory status
free -h
# Detailed memory info
cat /proc/meminfo
# Swap usage
swapon --show
# OOM killer logs
dmesg | grep -i 'oom\|killed process\|out of memory'
journalctl -k | grep -i 'oom\|killed'
If the available value in free -h drops below 10% of total RAM, you have memory pressure.
Step 2: Which Process Uses the Most Memory?
# Sort by memory usage with ps
ps aux --sort=-%mem | head -20
# Real-time monitoring with top
top
# Press 'M' to sort by memory
# More detailed memory analysis with smem
sudo apt install smem -y
smem -r -k | head -20
# Monitor a specific process
watch -n 2 'ps aux | grep app_name | grep -v grep'
Step 3: Confirm the Memory Leak
# Find the PID of a specific process
pgrep -f app_name
# Log memory usage every 30 seconds
while true; do
echo "$(date): $(cat /proc/PID/status | grep VmRSS)"
sleep 30
done
# Detailed memory map
cat /proc/PID/smaps | grep -E '(Rss|Pss|Shared|Private)' | awk '{sum += $2} END {print sum/1024 " MB"}'
Step 4: Application-Specific Solutions
Node.js Memory Leak
# Set Node.js memory limit
node --max-old-space-size=512 app.js
# PM2 auto-restart on memory limit
pm2 start app.js --max-memory-restart 500M
PHP-FPM Memory Leak
# /etc/php/8.x/fpm/pool.d/www.conf
# pm.max_requests = 500 # Restart worker after 500 requests
# PHP memory limit
# memory_limit = 256M
MySQL/MariaDB Memory Usage
# /etc/mysql/mysql.conf.d/mysqld.cnf
# innodb_buffer_pool_size = 256M # 25-50% of RAM
Step 5: Emergency Solutions
Clear Memory Cache
# Clear page cache (safe)
sync && echo 1 > /proc/sys/vm/drop_caches
Use drop_caches carefully in production. Disk I/O may temporarily increase after clearing the cache.
Restart the Leaking Service
# Restart service
sudo systemctl restart service_name
# Apply memory limit with systemd
# /etc/systemd/system/service.service
# [Service]
# MemoryMax=512M
Add Swap Space
# Create swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Conclusion
To resolve memory leaks, first identify which process is consuming memory with ps aux and smem, then confirm the leak by monitoring growth over time. In the short term, restart the leaking service and apply a memory limit with systemd. For a permanent fix, investigate the application's source code using tools like Valgrind (C/C++), heapdump (Node.js), or jmap (Java). Contact REXE support if the issue persists.
Related Articles
SSH Connection Problem: Can't Connect to Server
SSH connection troubleshooting guide: Path.net firewall rules, Default Block, creating a filter rule for SSH port, and step-by-step troubleshooting.
RDP Connection Problem: Can't Connect to Windows Server
RDP connection troubleshooting guide: Path.net firewall rules, Default Block, creating a filter rule for RDP port, and step-by-step troubleshooting.
How to Run an MTR Test and Submit Results to Support
Guide to running MTR network tests with WinMTR and Linux MTR, creating ICMP filter rules, and submitting results to support. Diagnose packet loss and latency.