Practical Tips for linux: Valgrind, a memory leak detection tool

Source: Internet
Author: User
Tags valgrind

Practical Tips for linux: Valgrind, a memory leak detection tool
1. Valgrind Introduction

1. When writing C/C ++, one of the most common errors is memory leakage. This problem is actually inseparable from the programming habits of programmers. If you can release the memory immediately after applying for memory space. This will greatly avoid Memory leakage. However, they are not sages, and they cannot be used. Sometimes we forget to release the memory and cause serious errors. Therefore, we need to use valgrind to determine the memory leakage.

Valgrind is a memory debugging tool for Linux (supporting x86, x86_64, and ppc32) programs. It can monitor the memory usage of compiled binary programs (malloc and free in C language, and C ++'s new and delete) to find out the memory leakage problem.

The Memcheck tool included in Valgrind can check the following program errors:

Use uninitialized memory (Use of uninitialised memory)
Use the released memory (Reading/writing memory after it has been free 'd)
Use the memory space that exceeds the malloc allocation (Reading/writing off the end of malloc 'd blocks)
Unauthorized stack access (Reading/writing inappropriate areas on the stack)
Whether the requested space is released (Memory leaks-where pointers to malloc 'd blocks are lost forever)
Match malloc/free/new/delete request and released memory (Mismatched use of malloc/new [] vs free/delete [])
Overlapping src and dst pointers in memcpy () and related functions)
Repeat free

2. Example of valgrind:

1. Find the tar package of valgrind on the official website:
Decompress and compile. After installation, make:


Compile to the specified location:


Install valgrind:


After that, we can check the program in the system. First, we use ls to display the files in the root directory. The ls command has no memory leakage:


To reflect valgrind's error checking function, we intentionally write a C program that only malloc is not free:


After compiling the link, use valgrind for detection and find Memory leakage:


Then we release the memory in the C program:


After the link is compiled again, use valgrind for detection:

This show that we applied for and released the space of B, reflecting the powerful role of valgrind.

3. Summary

It is good to have such a tool for checking errors, but I think programmers should always be clear-headed and know what their programs are doing. Remember this advice if your language does not have a garbage collection mechanism. Therefore, do not perform any operations after applying for a piece of memory. Release the memory in the appropriate position first. After all, it's not difficult to borrow money. Pai_^

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.