Linux Practical tips: Detecting memory leak tool Valgrind

Source: Internet
Author: User
Tags valgrind

1.ValgrindIntroduction

1. One of the most common mistakes when we write C + + is memory leaks, which are inextricably linked to programmers ' programming habits. If you are applying for memory space, you can immediately release the memory in the appropriate location. The case of a memory leak can be avoided to a great extent. But err, who can have no. Sometimes we do forget to release memory and cause more serious errors, so this time we need to use the Valgrind tool to determine the memory leak.

Valgrind is a memory debugging tool for Linux (x86, x86_64, and PPC32) programs that can monitor memory usage of the compiled binaries (malloc and free in C, and new and delete in the C + +). Identify memory leak issues.

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

   use of uninitialized memory(use of uninitialised memory)
   using memory that has already been freed(reading/writing memory after it had been free ' d)
   use more memory space than malloc allocates(Reading/writing off the end of malloc ' d blocks)
   illegal access to the stack(Reading/writing inappropriate areas on the stack)
   is there a free space for the application ?(Memory leaks–where pointers to malloc ' d blocks is lost forever)
   malloc/free/new/delete application and release memory matching(Mismatched use of malloc/new/new [] vs Free/delete/delete [])
   the overlap of src and DST (Overlapping src and DST pointers in memcpy () and related functions)
   Repeat Free

Examples of 2.valgrind usage:

1. Find the Valgrind tar package on the official website:
Unzip, compile. After completing the Make installation:


Compile to established location:


Install Valgrind:


After the completion of the system we can detect the program, first we use LS to display the root directory files, ls command is no memory leaks:


In order to embody the valgrind function, we deliberately write a C program that only malloc does not have free:


After the link is compiled, it is detected with Valgrind and a memory leak is found:


Then we release the memory in the C program:


After compiling the link again, use Valgrind to detect:

This time it shows that we have applied for and freed up 400B of space, reflecting the powerful role of Valgrind.

3. Summary

It's good to have such a tool, but I think programmers should always be clear-headed and know what their program is doing, and remember this advice if your language doesn't have a garbage collection mechanism. Then after applying for a piece of memory do not do anything, first in the appropriate location to free memory. After all, it is not difficult to borrow well. ^_^

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.