Valgrind基本用法
介绍
valgrind 是一套linux下的开源仿真调试工具集,遵循GPLv2许可协议,可以用于内存调试,内存泄漏检测以及性能分析。
valgrind包含下列一些工具:
Memcheck:内存错误检测器,这是最常用的工具,用于检测程序中的内存问题,如泄露、越界、非法指针等。memcheck会检测所有对内存的去写操作,一切对malloc()/free()/new/delete的调用都会被捕获,所以能检测以下问题:
对未初始化内存的使用;
读/写释放后的内存块;
读/写超出 malloc 分配的内存块;
读/写不适当的栈中内存块;
内存泄漏,指向一块内存的指针永远丢失;
不正确的 malloc/free 或 new/delete 匹配;
memcpy() 相关函数中的 dst 和 src 指针重叠。
Helgrind:它主要用来检查多线程程序中出现的竞争问题。Helgrind寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发觉的错误。Helgrind实现了名为Eraser的竞争检测算法,并做了进一步改进,减少了报告错误的次数。不过,Helgrind仍然处于实验状态。
Massif:堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。
Massif对内存的分配和释放做profile。程序开发者通过它可以深入了解程序的内存使用行为,从而对内存使用进行优化。这个功能对C++尤其有用,因为C++有很多隐藏的内存分配和释放。
安装
linux安装口令Debian 和 Ubuntu 可使用 apt 安装sudo apt install valgrind
实例
实例1:
1.新建一个memoryleak.c文件
新建一个c文件memoryleak.c,使用malloc申请一块内存空间,但是不释放,如下:
#include <stdio.h>#include <stdlib.h>int main() {int *arr = malloc(sizeof(int) * 100);return 0;}
2.编译c文件
gcc -g -o memoryleak memoryleak.c
3.运行valgrind
valgrind 默认是运行memcheckvalgrind ./memoryleak
4.运行结果显示
==320== Memcheck, a memory error detector==320== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.==320== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info==320== Command: ./memoryleak==320== ==320== ==320== HEAP SUMMARY:==320== in use at exit: 400 bytes in 1 blocks==320== total heap usage: 1 allocs, 0 frees, 400 bytes allocated==320== ==320== LEAK SUMMARY:==320== definitely lost: 400 bytes in 1 blocks==320== indirectly lost: 0 bytes in 0 blocks==320== possibly lost: 0 bytes in 0 blocks==320== still reachable: 0 bytes in 0 blocks==320== suppressed: 0 bytes in 0 blocks==320== Rerun with --leak-check=full to see details of leaked memory==320== ==320== For lists of detected and suppressed errors, rerun with: -s==320== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
可以看到第9行显示,申请了1个 400 字节的内存空间,但是没有释放。
添加--leak-check=full
参数,可以看到内存泄漏的更多细节。
==426== Memcheck, a memory error detector==426== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.==426== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info==426== Command: ./memoryleak==426== ==426== ==426== HEAP SUMMARY:==426== in use at exit: 400 bytes in 1 blocks==426== total heap usage: 1 allocs, 0 frees, 400 bytes allocated==426== ==426== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1==426== at 0x483F7B5: malloc (vg_replace_malloc.c:381)==426== by 0x10914A: main (memoryleak.c:6)==426== ==426== LEAK SUMMARY:==426== definitely lost: 400 bytes in 1 blocks==426== indirectly lost: 0 bytes in 0 blocks==426== possibly lost: 0 bytes in 0 blocks==426== still reachable: 0 bytes in 0 blocks==426== suppressed: 0 bytes in 0 blocks==426== ==426== For lists of detected and suppressed errors, rerun with: -s==426== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
可以看到第13行显示,内存泄漏发生在 memoryleak.c 的第6行代码。
实例2:
接着,我们增加一行代码修复内存泄漏
#include <stdio.h>#include <stdlib.h>int main() {int *arr = malloc(sizeof(int) * 100);free(arr);return 0;}
1.编译
gcc -g -o memoryleak memoryleak.c
2.运行valgrind
valgrind ./memoryleak
==484== Memcheck, a memory error detector
==484== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==484== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==484== Command: ./memoryleak
==484==
==484==
==484== HEAP SUMMARY:
==484== in use at exit: 0 bytes in 0 blocks
==484== total heap usage: 1 allocs, 1 frees, 400 bytes allocated
==484==
==484== All heap blocks were freed -- no leaks are possible
==484==
==484== For lists of detected and suppressed errors, rerun with: -s
==484== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
可以看到内存泄漏已经消除。
Valgrind基本用法相关推荐
- Valgrind简单用法
Valgrind的主要作者Julian Seward刚获得了今年的Google-O'Reilly开源大奖之一──Best Tool Maker.让我们一起来看一下他的作品.Valgrind是运行在Li ...
- Valgrind 基本用法
介绍 Valgrind 是一套 Linux 下的开源仿真调试工具集,遵循 GPLv2 许可协议,可用于内存调试.内存泄漏检测以及性能分析. Valgrind 由内核(core)以及基于内核的其他调试工 ...
- 一个内存增长问题的分析和处理(二)——valgrind工具的用法
valgrind是linux下对C++和C程序进行内存泄露检测的工具,除了内存检测,valgrind还提供了很多其他的功能,这里主要介绍下valgrind的内存检测的功能. 首先是文件的下载,valg ...
- 开源工具之valgrind
首先对源文件进行编译: Preparing your program Compile your program with -g to include debugging information so ...
- valgrind 的使用简介
一 valgrind是什么? Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框架 ...
- Valgrind 使用简单说明
Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的ma ...
- linux工具之检测内存泄漏-valgrind
0.前言 内存泄漏是c++程序常见的问题了,特别是服务类程序,当系统模块过多或者逻辑复杂后,很难通过代码看出内存泄漏: valgrind是一个开源的,检测c++程序内存泄漏有效工具,编译时加上-g选项 ...
- html.renderaction 控制器,Html.RenderAction简单用法
CATransition(os开发之画面切换) 的简单用法 CATransition 的简单用法 //引进CATransition 时要添加包"QuartzCore.framework&qu ...
- Valgrind 使用方法
Valgrind 介绍 Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视内存的使用情 ...
最新文章
- 【目标检测】yolo系列:从yolov1到yolov5之YOLOv3详解及复现
- 《如何阅读一本书》之 阅读的层次
- 用 Python 在朋友圈中游遍全球
- Windows Mobile下C++取屏幕分辨率的方法
- 如何在视图中启用thymeleaf
- VB 窗体实现文件拖拽获取路径方法
- kernel or user oops信息定位步骤
- 微博中微服务缓存_新浪微博温情:基于微服务的微博直播互动架构设计经验分享...
- 如何在微信小程序中使用echarts绘制地图(微信小程序制作疫情数据地图)
- 国美云运维自动化实践
- textfield观察UIControlEventEditingChanged时键盘快捷输入验证码会执行两次
- java实现doc内容对比_Java平台Word格式处理控件Spire.Doc8月新功能代码演示:比较两个 Word 文档的内容...
- 96Boards MIPI CSI Camera Mezzanine
- 为什么热咖啡保温几小时后的变化比冰咖啡大?
- 【接口调用】EasyCVR获取直播流接口调用过程
- CRM销售系统价格 CRM销售管理系统需要多少钱
- Vue中监听键盘事件及自定义键盘事件
- uniapp集成极光推送证书与描述文件申请
- 基于单片机音符和音乐电子琴仿真设计-毕设课设资料
- 对话 CTO | 喜茶也有 CTO?听陈霈霖讲讲茶饮中的技术甜度
热门文章
- 计算机新教师培训自我评价,教师个人自我评价(精选多篇)
- HTML相对定位与绝对定位
- table表格自动滚动
- tf.extract_image_patches以及pytorch的extract_patches
- 【自学Python】Python布尔型(bool)
- 全国计算机应用基础统考成绩查询,2020年12月网络教育统考成绩查询时间及入口...
- 10秒钟脱口而出两位数的平方
- 系统安全及应用--账号安全控制
- 云服务器购买按量付费实例,并搭建私有网络图解
- 喜欢NBA的朋友快来这里看第一阶段的全明星投票结果!!!(更新到第二阶段、第三阶段)