


How to troubleshoot memory leaks by process
Jul 28, 2025 am 01:41 AMWhen encountering a memory leak problem, you must first confirm whether it is a memory leak, and then locate the specific program through process analysis. The steps include: 1. Use top or htop to view the continuously growing RSS; 2. Use ps to filter high memory occupancy processes and record PID; 3. Check suspicious memory mapped areas through pmap; 4. Use Valgrind, gperftools, perf or pprof to deeply analyze memory behavior; 5. If necessary, set memory limits, restart services regularly or enable swap as temporary measures. The entire process needs to start with phenomena, capture data and gradually narrow the scope with tools, and finally return to the code level to solve the problem.
When encountering memory leaks, many people's first reaction is "It's over, the system is about to collapse." In fact, as long as you follow the process, the reason can be found in most cases. The key is to locate the specific program through process analysis and "secretly eating memory".
Check if it is a memory leak
Not all memory usage is a leak, so you must first distinguish it clearly. You can use top
or htop
to see which processes' memory usage has been rising, especially the RSS (Resident Set Size) column. If a process doesn't run for a long time but the memory continues to rise, it's likely to be a leak.
Common phenomena include:
- The app was killed while running.
- The system response is slow, swap is also widely used
-
oom-killer
related information appears in the log
Don't rush to optimize the code at this time. Take a few snapshots first to see which process is the most suspicious.
Find high memory usage processes
Under Linux, you can use ps
to quickly filter:
ps aux --sort=-%mem | head -n 10
This allows you to see the first few processes with the highest memory usage at the moment. Note their PIDs and use them later.
You can also use pmap
to view the detailed memory map of a process:
pmap -x <PID>
Focus on whether there are growing heap or anon regions. If you find that a library or mmap area is also expanding continuously, it is worth checking in depth.
Use tools to assist in analyzing memory behavior
Only relying on system commands can only see the appearance, and to go deeper, you have to rely on professional tools:
- Valgrind (massif) : Suitable for C/C programs, can generate detailed heap memory usage maps
- gperftools (tcmalloc) : can view the memory allocation call stack in real time, suitable for service programs
- perf : With the kernel profile function, it can track system-level memory events
- pprof : Good support for languages such as Go, Java, etc., graphically display the call path and memory allocation hotspots
For example, if you are using Go service, adding a parameter at startup can expose the pprof interface:
import _ "net/http/pprof" ... go func() { http.ListenAndServe(":6060", nil) }()
Then visit /debug/pprof/heap
to see the current heap memory allocation. Download it directly and use the pprof tool to analyze the call stack.
Temporary responses that can be tried
Sometimes you can't change the code immediately, but want to stabilize the online service, you can consider:
- Set memory limits for processes (cgroups or systemd)
- Regularly restart services (such as combined with cron or health checks)
- Enable swap (although it is not a cure for the symptoms)
These practices are just "stop bleeding" and not radical cure. To really solve it, you have to go back to the code level and check whether there are circular references, unreleased caches, and resource handles without close.
The process of troubleshooting memory leaks is a bit like a detective work. The key is to narrow the scope step by step through the process as the entry point. From observing phenomena to scrambling data, to tool-assisted analysis, every step must be based on it. There are many tools, just choose one you are familiar with. The key is to keep checking to the end. Basically that's it.
The above is the detailed content of How to troubleshoot memory leaks by process. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The pprof tool can be used to analyze the memory usage of Go applications and detect memory leaks. It provides memory profile generation, memory leak identification and real-time analysis capabilities. Generate a memory snapshot by using pprof.Parse and identify the data structures with the most memory allocations using the pprof-allocspace command. At the same time, pprof supports real-time analysis and provides endpoints to remotely access memory usage information.

Memory leaks can cause Go program memory to continuously increase by: closing resources that are no longer in use, such as files, network connections, and database connections. Use weak references to prevent memory leaks and target objects for garbage collection when they are no longer strongly referenced. Using go coroutine, the coroutine stack memory will be automatically released when exiting to avoid memory leaks.

Valgrind detects memory leaks and errors by simulating memory allocation and deallocation. To use it, follow these steps: Install Valgrind: Download and install the version for your operating system from the official website. Compile the program: Compile the program using Valgrind flags (such as gcc-g-omyprogrammyprogram.c-lstdc++). Analyze the program: Use the valgrind--leak-check=fullmyprogram command to analyze the compiled program. Check the output: Valgrind will generate a report after the program execution, showing memory leaks and error messages.

A memory leak in C++ means that the program allocates memory but forgets to release it, causing the memory to not be reused. Debugging techniques include using debuggers (such as Valgrind, GDB), inserting assertions, and using memory leak detector libraries (such as Boost.LeakDetector, MemorySanitizer). It demonstrates the use of Valgrind to detect memory leaks through practical cases, and proposes best practices to avoid memory leaks, including: always releasing allocated memory, using smart pointers, using memory management libraries, and performing regular memory checks.

Common memory leak scenarios in Java include: holding references to external objects, static references, invalid listeners, thread-local variables, and circular references. Common memory leak scenarios in application servers include threads holding references to servlet objects, static holders holding references to persistent connections, and listeners not being removed from components.

Debugging techniques for C++ multi-threaded programming include using a data race analyzer to detect read and write conflicts and using synchronization mechanisms (such as mutex locks) to resolve them. Use thread debugging tools to detect deadlocks and resolve them by avoiding nested locks and using deadlock detection mechanisms. Use the Data Race Analyzer to detect data races and resolve them by moving write operations into critical sections or using atomic operations. Use performance analysis tools to measure context switch frequency and resolve excessive overhead by reducing the number of threads, using thread pools, and offloading tasks.

Recently, "Black Myth: Wukong" has attracted huge attention around the world. The number of people online at the same time on each platform has reached a new high. This game has achieved great commercial success on multiple platforms. The Xbox version of "Black Myth: Wukong" has been postponed. Although "Black Myth: Wukong" has been released on PC and PS5 platforms, there has been no definite news about its Xbox version. It is understood that the official has confirmed that "Black Myth: Wukong" will be launched on the Xbox platform. However, the specific launch date has not yet been announced. It was recently reported that the Xbox version's delay was due to technical issues. According to a relevant blogger, he learned from communications with developers and "Xbox insiders" during Gamescom that the Xbox version of "Black Myth: Wukong" exists.

Thread safety and memory leaks in C++ In a multi-threaded environment, thread safety and memory leaks are crucial. Thread safety means that a data structure or function can be safely accessed in a concurrent environment, requiring the use of appropriate synchronization mechanisms. A memory leak occurs when allocated memory is not released, causing the program to occupy more and more memory. To prevent memory leaks, these best practices should be followed: Use smart pointers such as std::unique_ptr and std::shared_ptr to manage dynamic memory. Using RAII technology, resources are allocated when the object is created and released when the object is destroyed. Review code to identify potential memory leaks and use tools like Valgrind to detect leaks.
