How the C + + program accurately finds memory leaks

Source: Internet
Author: User

First, preface

Valgrind on the Linux platform can be very handy to help us locate the memory leak, because Linux in the development area of the use of the scene is mostly running server, coupled with its open source properties, relatively speaking, the problem is easy to form a "unified" standard. On the Windows platform, the debugging methods used by server and client developers are quite different. The following combination of my practical experience, to collate the common method of locating memory leaks.

Note: Our analysis premise is release version , because in the debug environment, through the VLD this library or the CRT library itself memory leak detection function can analyze the memory leak, relatively simple. And the server has many problems need to be on-line concurrency pressure situation only appears, so the discussion debug version debugging method is of little significance.

Second, the object count

Method: Count + + at object construction time, destructor--the number of objects to print at intervals

Pros: No performance overhead and virtually no additional memory consumption. Accurate positioning results.

Cons: Intrusive approach, you need to modify existing code, and for third-party libraries, STL containers, script leaks, etc. cannot be fixed because the code cannot be modified.

Third, reload new and delete

Method: Overload the New/delete, record the allocation point (even the call stack), and print periodically.

Pros: Not seen

Cons: Intrusive method, you need to add the header file to a large number of source file header, to ensure that overloaded macros can cover all new/delete. Record allocation points need to be locked (if your program is multithreaded), and the record allocation consumes a lot of memory (which is also the memory of the program).

Iv. Hook Windows System API

Method: Using Microsoft's detours library, Hook allocates the memory of the system Api:heapalloc/heaprealloc/heapfree (New/malloc of the underlying call), records the distribution points, and periodically prints.

Pros: Non-intrusive method, no need to modify existing files (Hook API, allocation and release go to their own hook function), check the overall, the third-party library, script library and so on can be counted.

Cons: Recording memory requires a lot of memory, and the multithreaded environment needs to be locked.

v. Use of DIAGLeak Detection

Microsoft produced a memory leak analysis tool, the principle of the same hookapi way. Display memory allocation data with Ldgraph visualization for easier discovery of leaks.

Configuring the release version within the IDE's project options also generates debug information, which is published together with the PDB file and EXE file.

After the program runs, open LeakDiag, set the symbol path

The memory allocation of the target process under the regular log, and the memory leak is discovered by ldgraph print allocation growth.

Pros: The same Hookapi method, non-intrusive modification, without any code changes. Tracking comprehensive. Visual analysis stack at a glance!

Cons: Impact on performance, hook allocation lock, traverse stack. However, the target process's own memory is not occupied.

Vi. Summary

For online production environments, it is recommended that large objects be judged by counts, with fast and accurate positioning and virtually no performance overhead. In the external testing phase, LeakDiag is used to assist the analysis, because the concurrency pressure is not too large, the performance cost can be affordable. On-line large-scale application phase, through the Hookapi method, combined with the GM instruction to control part of the time period of detection, so that the impact on the player (server performance degradation caused by delay) to a minimum.


The above method, I will follow the blog in detail, the text mentioned to the Detours library and LeakDiag tools, want to know the friends, can be codeprojet on the search under the relevant introduction, if you want to find information on Google, can also be here. If you have a new method can also leave a message to discuss, we work together to improve this series of introductory articles.


How the C + + program accurately finds memory leaks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.