Memory leaks in Linux are tricky and hard to notice. Your app start fine, dashboard look good. Then, day after day, it use little more RAM, and at some point Ubuntu server gets slow, and system shut down your application in middle of night. Logs do not say “memory leak”, you just see that service is down. You restart it, and everything works again. Until it happens again. This guide shows you how to find and fix memory leaks in Linux applications.
This guide is to give you clear way to deal with this problem. You will learn how to check if leak is real, find line of code that causes it, and fix it. All command here work on Ubuntu, and I tested it on Ubuntu 24.04 LTS.
Memory leak investigation depend on programming language and runtime used by application. This guide focus on general Linux monitoring tool and practical example for PHP and Node.js application.
TL;DR
- Memory leak occurs when application retain memory it no longer need, causing memory usage to grow unnecessarily over time.
- First, confirm leak with “free”, “ps”, and “pidstat”. Do not dig into code until you see steady growth.
- For PHP, watch your PHP-FPM worker with “ps” and log “memory_get_usage()” to find where growth start.
- To fix it, read data in small piece, put limit on cache, and close what you open.
- Until fix is ready, set “pm.max_requests” in PHP-FPM or systemd memory limit.
What Is Memory Leak?
Memory leak occur when application retain memory it no longer need or fail to release allocated memory appropriately. This can cause memory usage to grow over time and eventually reduce memory available to other application.
Some normal thing look like leak, but they are not. Linux use free RAM to cache files on purpose, so “full” memory bar does not mean there is problem. But cache with no size limit is leak in real life. Here is simple test to use:
- Normal use: memory goes up, then level off, then drop when load goes down.
- Cache growth: memory usage increase as data is cached but eventually stabilize because cache has maximum size or expiration policy. Unbounded cache may continue growing and cause memory pressure.
- Leak: memory usage continue to grow under similar workload without stabilizing, even after activity decrease. This may indicate memory leak, but further investigation is need to confirm cause.
Most common cause is missing “free()” in files and sockets that are never closed, lists that only grow, event listener in Node.js are never removed, and caches with no size limit.
How to Know If Your Ubuntu Server Has Memory Leak?
Before looking at any code, confirm leak with basic system tool. First, install one extra package you need:
sudo apt update
sudo apt install -y sysstat
This gives you “pidstat”. Other tool below are already on Ubuntu.
Check memory with “free” and “ps”
Start with big picture:
free -h
Look at “available” column, as it show how much memory your apps can still get, including cache that Linux can give back. If this number keep getting smaller over hours or days, something is holding on to memory.
Next, find which program use most memory:
ps aux --sort=-%mem | head -n 10
The “RSS” column show how much real RAM process is using right now. The “VSZ” column show virtual size. It is often very large even for healthy program, so do not worry about it.
Read Full Article: https://serveravatar.com/fix-memory-leaks



Top comments (0)