How to detect memory leakage in linux

Source: Internet
Author: User
Article Title: how to detect memory leaks in linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

  1. Development Background:

When using VC Programming in windows, we usually need to run the program in DEBUG mode. Then, when the debugger exits the program, it will print out the memory Information allocated on the stack while the program is running but not released, this includes the code file name, row number, and memory size. This function is a built-in mechanism provided by the MFC Framework and encapsulated in its class structure system.

In linux or unix, our C ++ program lacks proper means to detect memory information. Instead, we can only use the top command to observe the total amount of dynamic memory of the process. When the program exits, we cannot know any memory leakage information. To better assist Program Development in linux, we designed and implemented a memory detection subsystem in our class library project. The following describes the basic principles of new and delete in C ++, describes the implementation principles and techniques of the memory detection subsystem, and discusses the advanced topics of Memory Leak Detection.

  2. Principles of New and delete

When we write new and delete in the program, we actually call the new operator and delete operator built in the C ++ language. the so-called built-in language means that we cannot change its meaning, and its functions are always consistent. Taking new operator as an example, it always allocates enough memory before calling the corresponding constructor to initialize the memory. Delete operator always calls this type of destructor and then releases the memory (). What we can exert influence on is actually the method for allocating and releasing memory during the execution of new operator and delete operator.

The name of the function called by new operator for memory allocation is operator new, which is usually in the form of void * operator new (size_t size); the return value type is void *, this function returns an unprocessed (raw) pointer with uninitialized memory. The parameter size determines how much memory is allocated. You can add the extra parameter to overload function operator new, but the first parameter type must be size_t.

The delete operator is called to release the memory. Its name is operator delete, which is usually in the form of void operator delete (void * memoryToBeDeallocated). It releases the memory zone to which the input parameter points.

There is a problem here, that is, when we call new operator to allocate memory, there is a size parameter indicating the size of memory to be allocated. However, when the delete operator is called, there is no similar parameter. How does the delete operator know the size of the memory block to which the pointer is to be released? The answer is: for the system's own data types, the language itself can distinguish the size of memory blocks, and for Custom Data Types (such as our custom classes ), then, operator new and operator delete need to transmit information between each other.

When we use operator new to allocate memory for a user-defined object, the memory we actually get is larger than the memory of the actual object. In addition to storing object data, you also need to record the memory size. This method is called cookie. the implementation at this point is based on different compilers. (For example, MFC chooses to store the actual object data in the header of the allocated memory, while the latter part stores the boundary mark and memory size information. G ++ stores the relevant information in the first four parts of the allocated memory, and the actual data of the object is stored in the memory later .) When we use delete operator to release memory, delete operator can correctly release the memory block pointed to by the pointer based on this information.

The above discussion is about memory allocation/release for a single object. When we allocate/release memory for the array, although we still use new operator and delete operator, however, the internal behavior is different: new operator calls operator new [], the brother of the Number Group version of operator new, and then calls the constructor for each array member. Delete operator first calls the Destructor for each array member, and then calls operator delete [] to release the memory. Note that when we create or release an array consisting of custom data types, the compiler must identify the size of memory blocks to be released in operator delete, the cookie technology related to the compiler is also used.

To sum up, if we want to detect memory leaks, We must record and analyze the memory allocation and release in the program, that is, we need to reload operator new/operator new []; operator delete/operator delete [] Four global functions are used to intercept the memory operation information we need to check.

[1] [2] [3] [4] [5] Next page

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.