Tools for detecting memory leaks under Linux Valgrind

Source: Internet
Author: User
Tags valgrind

Reference:http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html

Almost copied from the reference, but the back of their own debugging code.

Here are some simple uses of Valgrind. more detailed ways to access Valgrind's home page: http://www.valgrind.org

Valgrind is the work of Julian Seward. Valgrind is a set of simulation-based program debugging and analysis tools running on Linux that contains a kernel, a software-synthesized CPU, and a series of gadgets.

Each tool can perform a task-debugging analysis or testing, and so on.

Valgrind can detect memory leaks and memory violations. You can also analyze the use of the cache, flexible and powerful, worth the start.

I. Overview of Valgrind

It mainly has the following tools.

1.Memcheck

Most commonly used to detect memory problems in the program, all memory reads and writes are detected, and all calls to malloc and free are captured, so it detects the following issues:

1) The use of memory for initialization

2) Read/write memory block after release

3) read/write memory blocks that exceed malloc allocations

4) Read/write inappropriate memory blocks in the stack

5) memory leak, pointer to a piece of memory forever lost

6) incorrect malloc/free or New/delete match

7) overlap of DST and src pointers in memcpy correlation functions

2.Callgrind

and Gprof similar analysis tools, but it to the operation of the program observation more nuanced, can provide us with more information. Unlike gprof, it does not need to add additional special options when compiling the source code, but it is recommended that the debug option be added.

Callgrind collects some data from the program runtime, establishes a function call graph, and optionally carries out a cache simulation. At the end of the run, it writes the parsing data to a file, Callgrind_annotate can convert the contents of the file into a readable form.

3.Cachegrind

The cache parser, which simulates the first-level cache I1,di and level two caches in the CPU, can pinpoint the loss and hit of the cache in the program.

It can also provide us with cache misses, memory references, and per-line code, each function, and the number of instructions generated by the entire program for each module, which is a great help to the optimizer.

4.Helgrind

Used to detect competition problems in multithreaded programs. Helgrind is looking for in-memory access to a thread without a consistently locked area. These areas are often situations where threads are out of sync and can cause difficult-to-dig errors.

Helgrind implements a competitive detection algorithm called "Eraser", and makes further improvements to reduce the number of reported errors. But Helgrinf is still in the experimental stage.

5.Massif

The stack analyzer, which measures how much memory the program uses in the stack, tells us the size of heap blocks, heap management blocks, and stacks. Massif can help us reduce the use of memory, in the virtual memory of the modern system, it can also speed up the operation of our programs, reduce the chance of the program stay in the swap area.

In addition, Lackey and Nulgrind are also available. Lackey is a small tool, seldom used; Nulgrind just shows developers how to create a tool.

Second, the use of Valgrind

Install first, my server is already installed, do not know is not all Linux comes with this thing.

The command format is as follows:

Valgrind [valgtind-options] Your-prog [Your-prog options]

Like what:

-H Display Help information

--version Display kernel version information (I don't know why it's not-V)

-Q quiet operation, only print error messages

-tool=[default:memcheck] The most commonly used option, followed by the tool name. If you omit the tool name, run Memcheck by default

For example, the following program:

#include <stdio.h>#include<stdlib.h>voidFun () {int*x =malloc(Ten*sizeof(int)); x[Ten] =0;}intMain () {inti = About;         Fun (); printf ("i =%d\n", i); return 0;}

There are two problems:

1) No resources for free applications

2) The fun function is out of bounds, x[10] is illegal

The following shows how to use Memcheck in Valgrind:

You can also add a tool when calling: $valgrind –tool=memcheck./malloc

The 28308 in ==28308== represents the process number at which the program runs.

Invalid Write of size 4: Indicates an illegal write, the following is a fun function called in main that tells us where the error occurred.

Heap SUMMARY: Describes the situation of the heap, you can see the application of 40 bytes, after that there are 1 applications, 0 are free.

LEAK SUMMARY: It is also said that the heap of leaks, obviously lost has 40 bytes.

If I in main is not assigned, there will be some other errors here, so you can try it yourself. This needs to be applied to the actual project in order to understand more.

Here is the error message that I did not assign to intercept the section:

Here are some other uses (I also follow the reference, specifically how to use the actual project also need to understand):

When I tested the following, I in the main function changed to No assignment:

1. Once an error occurs, Valgrind automatically launches the debugger (typically GDB):

2. Try the callgrind below:

You can see that a file was generated (green box). When Callgrind runs your program, you can also use Callgrind_control to observe the execution of the program without interfering with its operation:

The following shows how to view details:

3. Try again Cachegrind:

Above is the instruction cache, I1 and L2i cache access information, including the total number of visits, the number of lost, the loss rate.

The middle is the data cache, D1 and l2d cache access related information.

The following is a separate message for the L2 cache.

There is also an output file, cachegrind.out.25843, which can be viewed with cg_annotate. A detailed list is displayed.

Use of 4.missif

Similar to Cachegrind, except that the generated file is not the same, generated by the Massif.pid.ps PostScript file, there is only a copy of the stack to describe the use of a color map.

Tools for detecting memory leaks under Linux Valgrind

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.