Skip to main content
Back to Category

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.

Read time: 13 min Troubleshooting
memorymemory leakRAMvalgrindsmemlinuxperformance
Author
REXE Teknoloji Network & Security Team
Editor
REXE Teknoloji Technical Editorial
First published
Last updated

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 -h output gradually decreases
  • Swap usage increases
  • Application slows down over time
  • OOM (Out of Memory) killer activates and kills processes
  • oom-kill messages appear in dmesg

Step 1: Check Current Memory Status

hljs bash
# 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?

hljs bash
# 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

hljs bash
# 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

hljs bash
# 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

hljs bash
# /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

hljs bash
# /etc/mysql/mysql.conf.d/mysqld.cnf
# innodb_buffer_pool_size = 256M  # 25-50% of RAM

Step 5: Emergency Solutions

Clear Memory Cache

hljs bash
# 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

hljs bash
# Restart service
sudo systemctl restart service_name

# Apply memory limit with systemd
# /etc/systemd/system/service.service
# [Service]
# MemoryMax=512M

Add Swap Space

hljs bash
# 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.

Frequently Asked Questions

What is the difference between a memory leak and high memory usage?

High memory usage can be normal — the application may be processing a lot of data. With a memory leak, RAM usage continuously increases over time and never decreases. If it returns to normal after a server restart, you have a leak.

What is the OOM killer and why does it kill processes?

The OOM (Out of Memory) killer is a Linux kernel mechanism that automatically kills the most memory-hungry process to save the system when memory is exhausted. Use 'dmesg | grep oom' to see which process was killed.

Is the drop_caches command safe?

Clearing the page cache (echo 1) is generally safe, but temporarily increases disk I/O. Avoid using it during peak hours in production. This command does not cause permanent data loss.

How can I permanently fix a memory leak?

A permanent fix requires examining the application's source code. Use tools like Valgrind (C/C++), heapdump (Node.js), or jmap (Java) to find the source of the leak. In the short term, apply a MemoryMax limit with systemd and schedule regular restarts.

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.

8 min
sshconnection problemtroubleshooting
Network TrafficInbound GbpsOutbound Gbps