Linux next several programs memory leak check tool __linux

Source: Internet
Author: User
Tags valgrind

The reason to write this blog is because you are writing a Nginx disk cache management program, has now entered the testing phase, the test on this program is divided into several major steps: 1. Memory management is correct (because this program itself open up a lot of memory space for cache management, At the same time, the program itself is based on C/s + + development, memory management mechanism has been a headache for programmers 2. How well the program is (the basic requirement of any program is to meet the requirements of high concurrency, that is, if the basic requirements are not met, the program does not become a server) for 1th, here are some memory leak checking tools

Tools Description
Valgrind A powerful Open-source program detection Tool
Mtrace GNU extension, used to track Malloc,mtrace install hook function for memory allocation function (Malloc,rellaoc,memalign,free)
Dmalloc A tool used to check for memory leaks in C/s + +, that is, to check for memory that has not been released by the end of the program running, published in a runtime
Memwatch Like Dmalloc, it detects memory that is not freed, that the same memory is released multiple times, addresses access errors, and improperly uses unallocated memory areas.
Mpatrol A cross-platform C + + memory leak detector
Dbgmem It is also a form of dynamic library publishing with advantages similar to Dmalloc, but by contrast, it may be less characteristic
Electric Fence Not only can you track malloc () and free (), but you can also check read access and write to accurately indicate the instructions that caused the error.
Valgrind Detailed

Valgrind includes the following tools: 1.Memcheck: This is the most widely used tool for Valgrind, a heavyweight memory checker that can be used to discover the vast majority of memory errors in development, such as using uninitialized 2. Callgrind: It is primarily used to check the problems that arise during the invocation of a function in a program. 3.cachegrind: It is mainly used to check the problem of cache usage in the program 4.Helgrind: It is mainly used to check the competition problem that occurs in multiple threads 5.Massif: It is primarily used to check the problems that arise in the use of the stack in the program 6.Extension: You can use the features provided by the core to write your own specific memory Debugging Tools Linux Program memory space layout

Code snippet (. Text): Here is the CPU to execute the instructions, the code is shareable, the same code in memory only a copy, and this segment is read-only, to prevent the program due to error and modify its own command initialization data segment (. data). Here is a variable in the program that needs to be explicitly assigned an initial value, such as a global variable that is outside all functions: int val=100. It should be emphasized that both of these paragraphs are located in the executable file of the program, and the kernel reads it from the source program file when the EXEC function is invoked to start the program. Uninitialized data segment (. BSS). The data in this section that the kernel initializes to 0 or null before executing the program. For example, a global variable that appears outside of any function: int sum, and a global variable and a static local variable heap (HEAP) that are uninitialized or have an initial value of 0. This segment is used to make dynamic memory requests in a program, such as the Malloc,new series functions that are frequently used to request memory from this segment.
global variable and static local variable stack (stack) initialized with a value not 0. The local variables in the function and the temporary variables produced during the function call are saved in this section. Executable code, string literals, read-only variables. Memory Check principle

Memcheck to detect memory problems:

1.valid-value Table:

For each byte (byte) in the entire address space of the process, there are 8 bits corresponding to it, and for each register of the CPU, there is also a bit vector corresponding to it. These bits are responsible for recording whether the byte or register value has a valid, initialized value 2.valid-address table

For a byte (byte) in the entire address space of the process, and the corresponding 1bit, it is responsible for recording whether the address can be read or written. detection principle when you want to read and write a byte in memory, first check this byte corresponds to a bit. If the a bit shows that the position is invalid, Memcheck reports a read-write error. The kernel (core) is similar to a virtual CPU environment, so when a byte in memory is loaded into the real CPU, the corresponding v bit of the byte is also loaded into the virtual CPU environment, once the value in the register is used to generate the memory address, or the value can affect the output of the program. Memcheck checks for the corresponding vbits, and if the value has not been initialized, the use of uninitialized memory errors is reported.

Next I mainly introduce the installation and use of Valgrind, about other tools, we can go online to access the information, thank you for your cooperation. Valgrind Installation 1. Unpack the installation package

TAR-JXVF valgrind-3.11.0.tar.bz2  -c/usr/local/src
2. Access to directory Installation
cd/usr/local/src/valgrind-3.11.0
3. Run./autogen.sh setting up the environment (requires a standard autoconf tool)
4. Configure Valgrind to generate makefile files
./configure--prefix=/usr/local
5. Compile and install Valgrind
Make  && make install
Valgrind Use

Step One: Prepare the program.

In order to Valgrind found the error more accurate, such as the ability to navigate to the source code of the line, it is recommended to compile with the-g parameter, compile optimization options to choose O0 (do not optimize)

Step two: Under the Valgrind, run the executable program

Using Valgrind to debug a memory problem, you do not need to recompile the source program, its input is binary executable program. The common format for calling Valgrind is: Valgrind [valgrind-options] Your-prog [your-prog-options]
The parameters of Valgrind are divided into two categories, one is the core parameter, it is applicable to all the tools, and the other is a specific tool such as Memcheck parameters. Valgrind The default tool is Memcheck, or you can specify other tools through "–tool=tool name." Valgrind provides a large number of parameters to meet your specific debugging needs, specific reference to its user manual.


a summary of common memory problems found using Memcheck

Memcheck divides memory leaks into two types, one is a possible memory leak (possibly lost) and the other is a determined memory leak (definitely lost). Possibly lost refers to the existence of a pointer that is still able to access a block of memory, but that pointer is no longer the first address of that memory. Definitely lost refers to the ability to access this block of memory. The definitely lost is also divided into two types: direct and indirect (indirect). The direct and indirect difference is that there is no pointer to the memory directly, which means that the pointer to that memory is located at the memory leak. In the above example, the root node is directly lost, while the other nodes are indirectly lost

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.