Linux memory leakage detection (1) simplest method

Source: Internet
Author: User

Linux memory leakage detection (1) simplest method
What is memory leakage?

Memory leakage means that the memory is not released after the application is used, and thus cannot be recycled and reused by the operating system.
For example, in this program, 4 bytes of space is applied but not released, and 4 bytes of memory is leaked.

#include 
  
   using namespace std;int main(){     int *p = new int(1);     cout <<*p<
   
  

With the passage of time, more and more leaked memory, less and less available memory, performance is impaired, and system crashes.

In general, when Memory leakage occurs, the leaked memory can be recycled after restart. However, for linux, server programs are usually run and cannot be restarted at will. Therefore, you must be extremely careful when dealing with memory leaks.

Memory leakage Characteristics

Hard to reproduce-it takes a long enough time to be exposed.

Difficult to locate-the error location is random and cannot be seen any connection with the memory leakage code.

The simplest method

To avoid writing out a program with Memory leakage, this programming specification usually requires us to apply for and release the program during writing. Because each application means that one release must correspond to it.

Based on this feature, a simple method is to count the number of applications and releases in the Code. If the number of applications and releases is different, it is considered as a memory leak.

#include "stdio.h"#include "stdlib.h"int malloc_count, free_count;void * my_malloc(int size){ malloc_count++; return malloc(size);}void my_free(void *p){ free_count++; free(p);}int main(){ count = 0; int *p1 = (int *)my_malloc(sizeif(int)) int *p2 = (int *)my_malloc(sizeif(int)) printf("%d, %d", p1, p2); my_free(p1); if(malloc_count != free_count) printf("memory leak!\n"); return 0}
Method Analysis Advantages:

Intuitive, easy to understand, and easy to implement

Disadvantages:

1. This method requires that the printed analysis generated during running be known at the end of the running.

2. This method requires that all functions applied for and released space be encapsulated and modified to called encapsulated functions. Although there are not many interfaces for applying for/releasing memory in C, there are a lot of calls to these interfaces for a large project. It is a big workload to replace them all.

3. Applicable only to C language and not to C ++

4. It is not applicable to the called library. Modify the library code if you want to apply the Code to the database.

5. You can only check for leaks without specific information, such as how much space is leaked.

6. It cannot be specified which line of code caused the leakage.

Improvement

Although this method is simple, it has many shortcomings and cannot be applied to projects. I want to know how to improve and look at the next decomposition.

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.