zoukankan      html  css  js  c++  java
  • valgrind基本使用

    1。valgrind是一个内存检测工具,类似的还有purify,insure++等

    2。测试文件test.c

      test.c :

        main(){

          int* a=new int[100];

          return 0;

        }

      第一步:gcc -o test test.c

      第二步: valgrind --tool=memcheck --leak-check=yes ./test

      结果:

    ==3053== Memcheck, a memory error detector
    ==3053== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
    ==3053== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
    ==3053== Command: ./a.out
    ==3053==
    ==3053==
    ==3053== HEAP SUMMARY:
    ==3053== in use at exit: 400 bytes in 1 blocks
    ==3053== total heap usage: 1 allocs, 0 frees, 400 bytes allocated
    ==3053==
    ==3053== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==3053== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==3053== by 0x8048528: main (temp.cc:41)
    ==3053==
    ==3053== LEAK SUMMARY:
    ==3053== definitely lost: 400 bytes in 1 blocks
    ==3053== indirectly lost: 0 bytes in 0 blocks
    ==3053== possibly lost: 0 bytes in 0 blocks
    ==3053== still reachable: 0 bytes in 0 blocks
    ==3053== suppressed: 0 bytes in 0 blocks
    ==3053==
    ==3053== For counts of detected and suppressed errors, rerun with: -v
    ==3053== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

    分析:

      上面的==3053==,3053代表test的进程号

      从上面可看到 ,by 0x8048528:main(temp.cc:41)第41行用operator new分配了400字节内存但是没有释放,最后总结此进程泄露内存400字节

    3.将内存检测工具与gdb一起使用

      命令: valgrind --tool=memcheck --leak-check=yes --db-attach=yes ./test

      --db-attach默认调试器是GDB,可以使用--db-attach=<command>指定

  • 相关阅读:
    Thinkphp回顾(五)之前台模板中的基本语法
    Thinkphp回顾之(四)查询方法深入学习
    Thinkphp框架回顾(三)之怎么实现平常的sql操作数据库
    Thinkphp学习回顾(二)之config.php的配置
    Thinkphp学习回顾(一)之基本结构目录
    端口
    curl put delete post get请求类型参数
    xshell连接virtualbox下的linux系统
    实现jsonp的三种方式
    匹配汉字
  • 原文地址:https://www.cnblogs.com/buxianghe/p/3150068.html
Copyright © 2011-2022 走看看