Valgrind---Software development tool for memory debugging, memory leak detection and performance analysis

Source: Internet
Author: User
Tags memory usage valgrind macbook
ValgrindIt is a software development tool for memory debugging, memory leak detection and performance analysis. The name Valgrind is taken from the entrance of the temple of the fallen in Norse mythology. general use mode Valgrind--leak-check=full./a.out ValgrindThe original author was Julian Seward, who won the second Google-o ' Reilly Open source Code award in 2006 for his work on the development of Valgrind.

When "one" uses Valgrind for memory detection of your code, it is possible that some variables were not initialized if you were prompted to "Conditional jump or move depends on uninitialised value (s)."

For example, I encountered two such hints, one because the struct TM structure was uninitialized and the other was caused by the uninitialized Char tmp[512. To initialize, only memset can be done, and after that, Valgrind no longer prompts for a problem.

Please believe Valgrind in more than 90% of the time, instead of insisting that your code does not need to make any changes.


"2" 1. Overview 2. Valgrind 3. Memory leak monitoring 3.1. Example code 3.2. Compile it 3.3. Memory leaks with Valgrind monitoring process 4. Hanging pointer 4.1. Example code 4.2. Valgrind Run result 5. Release the same pointer 5.1 more than once. Example code 5.2. Valgrind monitoring 6. The advantages and disadvantages of Valgrind 6.1. Advantages 6.2. Disadvantages 7. Other tools for Valgrind 7.1. Cachegrind 7.2. Callgrind 7.3. Helgrind 7.4. DRD 7.5. Massif 7.6. Dhat 8. Reference 1 Overview

The following three memory problems often occur when you are programming with C + +: The memory leak suspension pointer releases the same piece of memory multiple times

This series of articles briefly describes the tools and methods for troubleshooting these three issues, first look at Valgrind 2 valgrind

Valgrind is a tool that can monitor memory usage and monitor memory leaks. For some applications that are not large in size, Valgrind is a sharp weapon. 3 memory leak monitoring 3.1 Sample Code

1:int Main ()
 2: {
 3:     char *p = malloc (sizeof (char) *);
 4:     if (p = = NULL) {
 5: return         0;
 6:     }
 7: 
 8:     *p++ = ' a ';
 9:     *p++ = ' B ';
One 
:     printf ("%s\n", *p);
: return     0;
14:}
3.2 Compiling it
1:gcc-g-O core1 core1.c
3.3 using Valgrind to monitor process memory leaks
1:valgrind--leak-check=yes--show-reachable=yes./core

The output of the Valgrind is:

 1: ==25500== Memcheck, a memory error detector 2: ==25500== Copyright (C) 2002-2009, and GNU GPL ' d, by Julian Seward et
 Al. 3: ==25500== Using Valgrind-3.5.0 and Libvex; Rerun with-h for copyright Info 4: ==25500== Command:./core1 5: ==25500== 6: ==25500== Conditional jump or move Depen DS on uninitialised value (s) 7: ==25500== in 0x36a104546a:vfprintf (in/lib64/libc-2.12.so) 8: ==25500== by 0x36a 104eac9:printf (in/lib64/libc-2.12.so) 9: ==25500== by 0x40055d:main (core1.c:13): ==25500==: (NULL) 12: ==25  500==: ==25500== HEAP summary:14: ==25500== in Use, exit:10 bytes in 1 blocks: ==25500== Total HEAP usage:  1 Allocs, 0 frees, Bytes allocated: ==25500== ==25500== bytes in 1 blocks are definitely lost in loss  1 of 1: ==25500== at 0x4a0515d:malloc (vg_replace_malloc.c:195): ==25500== by 0x400515:main (Core1.c:5) 20: ==25500==: ==25500== leak summary:22: ==25500== definitely lost:10 bytes in 1 blocks 23: ==25500== indirectly lost:0 bytes in 0 blocks: ==25500== possibly lost:0 bytes in 0 blocks 25: ==25500== Still reachable:0 bytes in 0 blocks: ==25500== suppressed:0 bytes in 0 blocks: ==25500== ==25500== F or counts of detected and suppressed errors, rerun with:-V: ==25500== use--track-origins=yes to where uninitialis
 Ed values come from: ==25500== ERROR summary:2 errors from 2 contexts (Suppressed:6 from 6)

As you can see, the VALGRIND hint that the memory allocated on line fifth has not been freed 4 hang pointer 4.1 sample code

1:struct Elem {
 2:     int     A;
 3:     double  B;
 4:};
 5: 
 6:int Main ()
 7: {
 8:     struct Elem *e = malloc (sizeof struct));
 9:     if (e = NULL) {
: return         0;
One:     e->a = ten;
:     e->b = 10.10;
:     Double *xx = &e->b;
:     printf ("%f\n", *xx);
: Free     (e);
:     printf ("%f\n", *xx);
: return     0;
25:}
4.2 Valgrind Run Results

Also the result of the Valgrind run with-G compiled:

 1: [cobbliu@macbook]$ valgrind--leak-check=yes--show-reachable=yes./core2 2: ==26148== Memcheck, a memory error Detec
 Tor 3: ==26148== Copyright (C) 2002-2009, and GNU GPL ' d, by Julian Seward et al. 4: ==26148== Using Valgrind-3.5.0 and Libvex; Rerun with-h for copyright Info 5: ==26148== Command:./core2 6: ==26148== 7:10.100000 8: ==26148== Invalid Read of Size 8 9: ==26148== at 0x4005ca:main (core2.c:26): ==26148== address 0x502a048 are 8 bytes inside a block of size Free ' d: ==26148== at 0x4a04d72:free (vg_replace_malloc.c:325): ==26148== by 0x4005c5:main (core2.c:24) 13
: ==26148== 14:10.100000: ==26148==: ==26148== HEAP summary:17: ==26148== in use at exit:0 bytes in 0 blocks ==26148== Total Heap Usage:1 Allocs, 1 frees, Bytes allocated (==26148==): ==26148== all heap blocks F  Reed-no leaks are possible: ==26148==: ==26148== for counts of detected and suppressed errors, rerun with:-V 23: ==26148== ERRORSummary:1 errors from 1 contexts (Suppressed:6 from 6)
 

You can see after free (e), the pointer xx became a hanging pointer, after the reading of XX, if XX point to the memory has not been glibc recycled, the process will not be core. Valgrind prompted in line 26 done to XX's Invalid read. 5 Release the same pointer multiple times 5.1 Sample Code

1:int Main ()
 2: {
 3:     char *p = malloc (sizeof (char) *);
 4:     if (p = = NULL) {
 5: return         0;
 6:     }
 7: 
 8:     char *q = p;
 9:     *p++ = ' a ';
One:     *p++ = ' B ';
:     printf ("%s\n", *p);
: Free     (p);
: Free     (q);
: return     0;
18:}
5.2 Valgrind Monitoring
 1: [cobbliu@macbook]$ valgrind--leak-check=yes--show-reachable=yes./core1 2: ==26874== Memcheck, a memory error Detec
 Tor 3: ==26874== Copyright (C) 2002-2009, and GNU GPL ' d, by Julian Seward et al. 4: ==26874== Using Valgrind-3.5.0 and Libvex; Rerun with-h for copyright Info 5: ==26874== Command:./core1 6: ==26874== 7: ==26874== Conditional jump or move Depen DS on uninitialised value (s) 8: ==26874== in 0x36a104546a:vfprintf (in/lib64/libc-2.12.so) 9: ==26874== by 0x36a 104eac9:printf (in/lib64/libc-2.12.so): ==26874== by 0x4005b5:main (core1.c:15) One: ==26874== (NULL) 13: ==26  874== Invalid Free ()/delete/delete[]-==26874== at 0x4a04d72:free (vg_replace_malloc.c:325): ==26874== by    0x4005c1:main (CORE1.C:17): ==26874== address 0x502a042 are 2 bytes inside a block of size Alloc ' d 17: ==26874== At 0x4a0515d:malloc (vg_replace_malloc.c:195): ==26874== by 0x400565:main (core1.c:5) 19: ==26874== 20: ==26874= =: ==26874== HEAP summary:22: ==26874== in-exit:0 bytes in 0 blocks: ==26874== Total heap Usage:1 Allocs, 2 frees, 10 Bytes Allocated: ==26874== ==26874== all heap blocks were freed--no leaks are possible 26: ==26874== 27: ==26874 = = for counts of detected and suppressed errors, rerun with:-V: ==26874== use--track-origins=yes to where Uniniti
 Alised values come from: ==26874== ERROR summary:2 errors from 2 contexts (Suppressed:6 from 6)

As you can see, the VALGRIND hint has a invalid free () 6 Valgrind in line 17

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.