Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具

官网:http://valgrind.org/

用户开发手册地址:http://valgrind.org/docs/manual/manual.html

下载安装步骤:

① git clone git@github.com:meihao1203/Valgrind

或:git clone https://github.com/meihao1203/Valgrind

② tar -xvzf valgrind-3.13.0.tar.gz

③ cd valgrind-3.13.0

④ make

⑤ sudo make install

查看是否安装成功:

valgrind --version

显示 valgrind-3.13.0 即为成功

查看帮助:

valgrind --help

Demo小例子
1  ///
2 /// @file uninit.cpp
3 /// @author meihao1203(meihao19931203@outlook.com)
4 /// @date 2018-07-12 19:59:17
5 ///
6
7 #include<iostream>
8 using namespace std;
9 int main(int argc,char** argv)
10 {
11 int* arr = new int[5]; //没有释放,内存泄露
12 return 0;
13 }
编译:

g++ -g uninit.cpp

使用valgrind调试,显示出详细的内存泄露信息,以及错误发生在哪一行:

valgrind --tool=memcheck --leak-check=full ./a.out

//==18626== Memcheck, a memory error detector
//==18626== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
//==18626== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
//==18626== Command: ./a.out
//==18626==
//==18626==
//==18626== HEAP SUMMARY:
//==18626== in use at exit: 72,724 bytes in 2 blocks
//==18626== total heap usage: 2 allocs, 0 frees, 72,724 bytes allocated
//==18626==
//==18626== 20 bytes in 1 blocks are definitely lost in loss record 1 of 2
//==18626== at 0x4C2E8BB: operator new[](unsigned long) (vg_replace_malloc.c:423)
//==18626== by 0x400717: main (uninit.cpp:11)
//==18626==
//==18626== LEAK SUMMARY:
//==18626== definitely lost: 20 bytes in 1 blocks
//==18626== indirectly lost: 0 bytes in 0 blocks
//==18626== possibly lost: 0 bytes in 0 blocks
//==18626== still reachable: 72,704 bytes in 1 blocks
//==18626== suppressed: 0 bytes in 0 blocks
//==18626== Reachable blocks (those to which a pointer was found) are not shown.
//==18626== To see them, rerun with: --leak-check=full --show-leak-kinds=all
//==18626==
//==18626== For counts of detected and suppressed errors, rerun with: -v
//==18626== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
valgrind --help对应的中文:
用法: valgrind [options] prog-and-args
[options]: 常用选项,适用于所有Valgrind工具 -tool=<name> 最常用的选项。运行 valgrind中名为toolname的工具。默认memcheck。
memcheck ------> 这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。
callgrind ------> 它主要用来检查程序中函数调用过程中出现的问题。
cachegrind ------> 它主要用来检查程序中缓存使用出现的问题。
helgrind ------> 它主要用来检查多线程程序中出现的竞争问题。
massif ------> 它主要用来检查程序中堆栈使用中出现的问题。
extension ------> 可以利用core提供的功能,自己编写特定的内存调试工具 -h –help 显示帮助信息。
-version 显示valgrind内核的版本,每个工具都有各自的版本。
-q –quiet 安静地运行,只打印错误信息。
-v –verbose 更详细的信息, 增加错误数统计。
-trace-children=no|yes 跟踪子线程? [no]
-track-fds=no|yes 跟踪打开的文件描述?[no]
-time-stamp=no|yes 增加时间戳到LOG信息? [no]
-log-fd=<number> 输出LOG到描述符文件 [2=stderr]
-log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
-log-file-exactly=<file> 输出LOG信息到 file
-log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]
-log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port LOG信息输出 -xml=yes 将信息以xml格式输出,只有memcheck可用
-num-callers=<number> show <number> callers in stack traces [12]
-error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]
-error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]
-db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]
-db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p] 适用于Memcheck工具的相关选项: -leak-check=no|summary|full 要求对leak给出详细信息? [summary]
-leak-resolution=low|med|high how much bt merging in leak check [low]
-show-reachable=no|yes show reachable blocks in leak check? [no]

Valgrind,内存调试工具的更多相关文章

  1. valgrind 内存调试工具

    一.valgrind 是运行在linux系统下的内存调试工具,支持很多对象:memcheck.addrcheck.cachegrind.Massif.helgrind.Callgrind等.使用val ...

  2. 初试valgrind内存调试工具

    虽然GDB调试工具功能强大,但对于平时做题调试的使用并不方便,这里尝试学习使用比较简单的valgrind工具 Valgrind是一个提供程序调试及性能分析的工具集.其包含的工具主要有Memcheck, ...

  3. valgrind 内存检测与调用图生成

    http://blog.csdn.net/destina/article/details/6198443  感谢作者的分享! 一  valgrind是什么? Valgrind是一套Linux下,开放源 ...

  4. c++Valgrind内存检测工具---19

    原创博文,转载请标明出处--周学伟  http://www.cnblogs.com/zxouxuewei/ 一.Valgrind 概述 Valgrind是一套Linux下,开放源代码(GPL V2)的 ...

  5. Valgrind 内存泄漏工具

    Valgrind 是一款 Linux下(支持 x86.x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的malloc和free,以及C++中的new和 ...

  6. 内存调试工具Electric Fence

    源码下载地址 注:官方地址下载不了,可能不再维护了,此是一个老项目 efence中相关环境变量控制: 302 /* 303 * See if the user wants to allow mallo ...

  7. Qt Creator Valgrind内存分析前端(分析Nginx内存)

    Linux上使用Qt Creator进行C/C++开发http://my.oschina.net/eechen/blog/166969Qt Creator GDB调试前端(调试Nginx):http: ...

  8. JVM常用启动参数+常用内存调试工具

    一.JVM常用启动参数 -Xms:设置堆的最小值. -Xmx:设置堆的最大值. -Xmn:设置新生代的大小. -Xss:设置每个线程的栈大小. -XX:NewSize:设置新生代的初始值. -XX:M ...

  9. linux下内存调试工具——valgrind

    1.valgrind之memcheck  最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc()/free()/new/delete的调用都会被捕获.所以,它 ...

随机推荐

  1. Codeforces 1006 F - Xor-Paths

    F - Xor-Path 思路: 双向搜索dfs 如果普通的搜索复杂度是n 那么双向搜索复杂度是√n 代码: #include<bits/stdc++.h> using namespace ...

  2. python - selenium 2 升级到最新版本

    python - selenium 2 升级到最新版本 之前一直用的是selenium 2.48 .firefox36 而实际用户的浏览器可能都有自动更新功能,所以版本基本上是最新的.所以这次专门做了 ...

  3. vs2015多行注释与取消多行注释

    注释: 先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U

  4. LeetCode--268--缺失数字

    问题描述: 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9 ...

  5. linux权限管理之文件属性

    文件属性 chattr ======================================================== 文件权限管理之: 文件属性注:设置文件属性(权限),针对所有用 ...

  6. 20190102xlVBA_多表按姓名同时拆分

    Sub 多表按姓名同时拆分20190102() AppSettings Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA ...

  7. summary ranges leetcode java

    问题描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...

  8. 『TensotFlow』RNN中文文本_上

    中文文字预处理流程 文本处理 读取+去除特殊符号 按照字段长度排序 辅助数据结构生成 生成 {字符:出现次数} 字典 生成按出现次数排序好的字符list 生成 {字符:序号} 字典 生成序号list ...

  9. MVC实战之排球计分(三)—— 模型类的设计与实现

    此软件使用的数据库连接方式code first 由EF框架产生数据库. code first需要对模型类设计和实现.模型类是现实实体在计算机中的表示.它贯穿于整个架构, 负担着在各层次及模块间传递数据 ...

  10. vue项目中引入第三方框架

    element-ui npm install element-ui -- save; main.js中 import Element from 'element-ui'; import 'elemen ...