Diagnosing and Resolving an Nginx Memory Leak on Linux Using Common Tools
This guide walks through identifying a 4 GB memory leak in an Nginx worker process on Linux, using tools such as top, ps, dmesg, pmap, /proc/*/smaps, and gdb to pinpoint the faulty extension and apply a fix.
A colleague reported that an Nginx instance on a server was consuming 4 GB of RAM, prompting an immediate investigation.
Step 1: Run top -u work to verify overall memory usage and confirm the 4 GB consumption.
Step 2: Use ps -ef | grep nginx | grep -v grep | grep work to locate the specific Nginx worker processes and identify which ones are using excessive memory.
Step 3: Examine error.log to understand why only certain workers show large memory footprints, indicating that the issue is not uniform across all workers.
Step 4: Check system logs with dmesg | grep pid to see that low‑memory workers are being OOM‑killed and the master process is respawning new workers.
Step 5: After confirming the leaking process, inspect its memory allocation:
Dump detailed allocation with pmap -x <pid>.
View memory segment boundaries via cat /proc/<pid>/smaps.
Attach with gdb -p <pid> and dump binary memory, e.g.,
dump binary memory ./memory2.log 0x7fa1d0b57000 0x7FA1D0B70000.
Step 6: Analyze the dumped data and discover that a custom Nginx extension storing business data was responsible for the leak; fixing the extension and redeploying resolves the issue.
Common Linux process memory analysis commands: top – shows overall and per‑process memory usage (RES, SHR, VIRT, DATA). pmap -x <pid> – displays detailed memory map of a process. mtrace – traces memory allocation calls. gdb -p <pid> – attaches to a running process for debugging. cat /proc/<pid>/smaps – reveals start/end addresses of memory regions.
dump binary memory ./out.log <start_addr> <end_addr>– extracts raw memory contents.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
