Five methods for C ++ Memory leakage check

Source: Internet
Author: User

Five methods for C ++ Memory leakage check

I. Preface

On the Linux platform, valgrind can help us easily locate memory leaks, because most of the use cases of Linux in the development field are running servers, coupled with its open-source attributes, relatively speaking, handling problems easily forms a "unified" standard. On the Windows platform, the debugging methods used by server and client developers are quite different. The following describes common methods for locating memory leaks based on my actual experience.

Note:Our analysis is based on the Release version.In the Debug environment, the memory leakage detection function of the VLD library or the CRT library can be used to analyze the memory leakage, which is relatively simple. The server has many problems that need to occur only when the online concurrency is under pressure. Therefore, it is of little significance to discuss the Debug method.

Ii. Object count

Method: print the number of objects at intervals of time during object construction, such as ++ and destructor.

Advantage: no performance overhead, almost no additional memory. The positioning result is accurate.

Disadvantage: the intrusive method needs to modify the existing code and cannot be located for third-party libraries, STL containers, and script leaks because the Code cannot be modified.

Iii. Reload new and delete

Method: Reload new/delete, record distribution point or even call stack), and print regularly.

Advantage: not seen

Disadvantage: the intrusive method adds the header file to the header of a large number of source files to ensure that the overloaded macro can overwrite all new/delete files. Record distribution points need to be locked if your program is multi-threaded), and record allocation requires a large amount of memory, which is also occupied by the program memory ).

Iv. Hook Windows System APIs

Method: Use the Microsoft detours library to hook the system Api for memory allocation: underlying calls of HeapAlloc/HeapRealloc/HeapFreenew/malloc), record the allocation points, and print them regularly.

Advantage: non-intrusive methods, without having to modify the existing file hook api, allocate and release them to your own hook function), perform comprehensive checks, and collect statistics on third-party libraries and script libraries.

Disadvantage: The Record Memory requires a large amount of memory, and the multi-threaded environment requires locking.

V. Use DiagLeak Detection

The memory leak analysis tool developed by Microsoft works in the same way as hookapi. Use LDGraph to visually display memory allocation data, making it easier to find leaks.

1. Configure the Release version in the IDE project option to generate debugging information. When publishing, publish the pdb file and the exe file together.

2. After the program runs, open LeakDiag and set the Symbol path.

3. Regularly Log the memory allocation of the target process, and use LDGraph to print the allocation growth to detect memory leakage.

Advantage: similar to the hookapi method, this method does not require any code changes. Comprehensive tracking. The visual analysis stack is at a glance!

Disadvantage: it has an impact on performance. hook allocation locks and stack traversal. But it does not occupy the memory of the target process.

Vi. Summary

For online production environments, it is recommended that large objects be determined by counting to locate quickly and accurately, with almost no performance overhead. In the external testing stage, LeakDiag is used to assist analysis, because the concurrency pressure is not too high at this time, and the performance overhead is still acceptable. In the online large-scale application phase, the HookApi method and GM command can be used to control the detection of some time periods, which can minimize the latency caused by decreased server performance of players.

Related Article

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.