Linux detects memory leak tool Valgrind__linux

Source: Internet
Author: User
Tags garbage collection valgrind

1.Valgrind Introduction

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 right place. Can greatly avoid memory leaks. But err, who can not. Sometimes we do forget to release the memory and cause a more serious error, so this time we need to use the Valgrind tool to determine the memory leak.

Valgrind is a Linux (support x86, x86_64 and PPC32) program Memory Debugging tool, it can be compiled binary program memory usage monitoring (C language of malloc and free, and C + + new and delete), Identify the memory leak problem.

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

Using uninitialized memory (use of uninitialised memory)

Use memory that has been freed (reading/writing memory after it has been free ' d)

Use more than malloc allocated memory space (reading/writing off the end of malloc ' d blocks)

Illegal access to stack (reading/writing inappropriate areas on the stack)

Whether the requested space is released (Memory leaks–where pointers to malloc ' d blocks are lost)

Malloc/free/new/delete application and free memory matching (mismatched use of malloc/new/new [] vs Free/delete/delete [])

Overlap between src and DST (overlapping SRC and DST pointers in memcpy () and related functions)

Repeat free

Overview of the use of Valgrind under Linux http://www.linuxidc.com/Linux/2012-08/67990.htm

Memory leak detection and performance analysis using Valgrind tools under Linux http://www.linuxidc.com/Linux/2012-06/63754.htm

Ubuntu Memory Leak Detection Tool Valgrind installation http://www.linuxidc.com/Linux/2011-12/49988.htm

Memory debugging and Code anatomy tools under Valgrind--linux http://www.linuxidc.com/Linux/2011-06/36907.htm

Application Valgrind to discover the memory problem of Linux programs [graphics and text] http://www.linuxidc.com/Linux/2008-11/17502.htm

Examples of 2.valgrind use:

1. Find Valgrind's tar packets on the official web:

Unzip, compile. Make installation after completion:

Compile to Development 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:

For more information please continue to read the next page highlights : http://www.linuxidc.com/Linux/2014-09/106299p2.htm

In order to reflect the Valgrind error function, we deliberately write a malloc no free C program:

After compiling the link, using Valgrind for detection, a memory leak was found:

Then we release the memory in the C program:

Compile the link again and use Valgrind detection:

This time it shows that we have applied and released 400B of Space, which embodies the powerful role of Valgrind.

3. Summary

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

This article permanently updates the link address : http://www.linuxidc.com/Linux/2014-09/106299.htm

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.