Introduction to Memory Leak

Source: Internet
Author: User

I believe that almost everyone has encountered the memory leak issue. Different solutions are available.

1. Prevent Memory leakage

For example, auto_ptr is used in C ++ and Java garbage collection is used. Memory Leak is generally not a problem in Java and Python programming languages. The most serious problem in these languages is the lack of memory. These Java programs need to handle insufficient memory-xmx1024m or enable larger memory, or adjust GC when you are bored. the solution for python is similar, but it can be explicitly deleted. in addition, I heard that python has insufficient memory allocation when calling Django, because Django is not released for SQL queries when DEBUG = true. Next, let's talk about the language C ++/C that requires your care. C ++ has auto_ptr, which is acceptable because it complies with raiI (resource acquirement is initial and does not know whether it is correct in English), that is, it applies for resources during class initialization, release resources during class analysis. Note that the Destructor is virtual. Why? This is because the Destructor parent class needs to be called with polymorphism. If the class you write is not that standard, you like to apply for resources everywhere and release resources everywhere. We recommend that you use the auto_ptr class, which is in <memory>. In fact, auto_ptr is similar to the followingCode:

Template <class T> <br/> class simple_auto_ptr {<br/> PRIVATE: <br/> T * point; <br/> public: <br/> explicit simple_auto_ptr (T * point): Point (point) {}< br/> virtual ~ Simple_auto_ptr () {delete point ;}< br/>}; <br/> Call Code such as simple_auto_ptr <int> A (New int (3 ));

The role of this stuff is to simplify the following code:

Void Foo () {<br/> T * A = new T (); <br/> try {<br/> throw something; <br/>} catch (something) {<br/> If (null! = A) {<br/> Delete A; <br/> A = NULL; <br/>}< br/> If (null! = A) <br/> Delete A; <br/>}< br/> Use auto_ptr to simplify the preceding process to <br/> void Foo () {<br/> auto_ptr <t> A (New T (); <br/> throw something; <br/>}

It can be said that it is still useful... but it cannot be used to apply for an array... unless you want to apply for an array of auto_ptr. In addition, it is said that it cannot be used in the container class. Therefore, you need to be careful to prevent memory leakage.

2. Monitor Memory leakage and use a small tool to monitor memory leakage

There are also some monitoring tools that can help you check for some memory leaks. Some tools simply replace free/malloc/realloc/calloc or new/Delete. With macro definition, you can easily:

# Define new my_new <br/> # define my_new <br/>

Then we can intercept the new/delete function calls. If you want to do more, it depends on your macro definition and the specific Writing Method of your function. Then it is like matching between the left and right brackets. Of course, it is often not that simple. For example, some pointers in the class also have some memory detection which requires you to pass in the pointer and then catch the pointer for some monitoring work.

However, some memory leak problems are hard to be detected, or sometimes they are falsely reported. For example, a pointer applies for space in the function and passes it out. Then, it performs n conversions outside, adds and sees the operation, and finally changes it to another pointer. Unless the detection tool converts the name, it can perform mathematical operations, such as int * A = new int [20]; int * c = ++ A; Delete [] -- C; of course, this is just a simulation. No one will write code like this. But the realProgramIt is very likely that this is the case after the code is simplified. Of course, dynamic_cast is more complex. You also need to determine whether the Destructor is a virtual function.

3. Assertion Method

Test Program correctness is difficult to monitor memory leakage.

The following uses a linked list as an example: When list_insert () is used, the original list has one element less than the current list. When list_delete () is used, the original list has one more element than the current list.

List * l = new list (); <br/> size_t old = L-> size (); <br/> L-> list_insert (some elements ); <br/> size_t new = L-> size (); <br/> assert (New-old = 1 ); <br/> old = L-> size (); <br/> L-> list_delete (some elements); <br/> New = L-> size (); <br/> assert (old-new = 1); <br/> // you must ensure that the size of the list is not related to the specific value. It should be the result of traversal.

Even if the size change here is correct, this still does not ensure that the appropriate Delete method is called in list_delete (). It is possible that the delete operation is not performed or the node is deleted, however, the node destructor in the linked list are not written, which may cause memory leakage. I personally encountered the problem of deleting the linked list function in PG. The length of the original linked list is indeed reduced. However, the memory of the linked list node is indeed not released.

4. Simulation Method

This method is actually very useful. It is generally determined that it may be a problem with a large module, but it is not clear the possible internal path, or it may be that the path is too complicated. Here we can simulate it. Simplify various processes and only call several major functions that apply to release resources. Then, the possibility of each major function is ruled out one by one. For example, if the chain table has many complex chain table operations in the original business flow, but the space to be released is actually list_insert () and list_delete (), you can call list_new () and list_delete () Thousands of times to check the situation. memory leakage is obvious.

 

 

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.