admin 管理员组文章数量: 1184232
(1)
algrind 3.10.0 是在 2015 年发布的,其对 CentOS 7 的兼容性较好,适用于 CentOS 7.4.1708 。Valgrind 3.11.0 则在 2016 年发布,同样支持 CentOS 7 系统。 求证真实性 哪个更稳定呢
关于 Valgrind 在 CentOS 7.4.1708(2017年发布) 上的兼容性和稳定性问题,经过考证和分析如下:
1. Valgrind 版本发布时间核实
-
Valgrind 3.10.0:
-
实际发布时间:2014年10月(非2015年)
-
主要改进:支持更多指令集(如 AVX2)、修复内存检查工具(Memcheck)的误报问题。
-
源码包:
valgrind-3.10.0.tar.bz2
-
-
Valgrind 3.11.0:
-
实际发布时间:2015年11月(非2016年)
-
主要改进:增强对 GCC 5.x 的支持、优化性能分析工具(Callgrind)。
-
源码包:
valgrind-3.11.0.tar.bz2
-
📌 结论:原描述中的发布时间有误,正确时间应为 3.10.0(2014年) 和 3.11.0(2015年)。
2. 对 CentOS 7.4.1708 的兼容性
CentOS 7.4 默认使用 GCC 4.8.5 和 GLIBC 2.17,与 Valgrind 的兼容性如下:
-
Valgrind 3.10.0:
-
完全兼容,CentOS 7.4 的官方仓库(
yum install valgrind)默认提供此版本。 -
稳定性:长期测试无严重问题,适合生产环境。
-
-
Valgrind 3.11.0:
-
兼容但需手动编译(CentOS 7 官方仓库未收录此版本)。
-
稳定性:修复了 3.10.0 的部分 Bug,但对老旧工具链(如 GCC 4.8.5)可能存在边际效应。
-
3. 稳定性对比
| 版本 | 优势 | 潜在问题 | 推荐场景 |
|---|---|---|---|
| 3.10.0 | 官方仓库直接安装,无需编译 | 部分新特性缺失(如 AVX2 优化) | 生产环境、稳定优先 |
| 3.11.0 | 支持更多新编译器(GCC 5+) | 需手动编译,可能依赖更新 | 开发环境、需最新功能 |
4. 实测建议
-
优先使用 Valgrind 3.10.0:
bash
下载
yum install valgrind # 直接安装官方稳定版
-
如需 3.11.0:手动编译并测试:
bash
下载
wget https://sourceware/pub/valgrind/valgrind-3.11.0.tar.bz2 tar -xjf valgrind-3.11.0.tar.bz2 cd valgrind-3.11.0 ./configure && make && make install
5. 版本选择总结
-
求稳选 3.10.0:CentOS 7.4 官方维护版本,无编译风险。
-
尝新选 3.11.0:适合需要新特性的场景,但需自行验证兼容性。
🔍 验证命令:安装后运行
valgrind --version确认版本,并通过简单测试(如valgrind ls)检查基础功能。
(2)验证 阶段
写一个最简单的 c语言内训泄露程序 并用valgrind-3.15.0 工具辅助代码测试
1. 最简单的 C 语言内存泄漏程序
以下代码演示了 动态内存分配后未释放(内存泄漏)的情况:
c
#include <stdlib.h>
#include <stdio.h>
void leak_memory() {
int *ptr = (int *)malloc(10 * sizeof(int)); // 分配内存但未释放
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return;
}
for (int i = 0; i < 10; i++) {
ptr[i] = i; // 写入数据(模拟正常使用)
}
// 忘记调用 free(ptr);
}
int main() {
printf("Running memory leak example...\n");
leak_memory();
printf("Memory leaked!\n");
return 0;
}
实测代码:memory.c
#include <stdlib.h>
#include <stdio.h>
void leak_memory() {
int *ptr = (int *)malloc(10 * sizeof(int)); // 分配内存但未释放
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return;
}
for (int i = 0; i < 10; i++) {
ptr[i] = i; // 写入数据(模拟正常使用)
}
// 忘记调用 free(ptr);
}
int main() {
printf("Running memory leak example...\n");
leak_memory();
printf("Memory leaked!\n");
return 0;
}
代码说明:
-
malloc(10 * sizeof(int))分配了 40 字节(假设int为 4 字节)的内存。 -
函数
leak_memory()退出时未调用free(ptr),导致内存泄漏。 -
程序编译运行不会报错,但 Valgrind 可以检测到泄漏。
2. 使用 Valgrind 3.15.0 检测内存泄漏
(1)编译代码(启用调试符号)
bash
下载
gcc -g -o mem_leak mem_leak.c # -g 生成调试信息
(2)运行 Valgrind 检测
bash
下载
valgrind --leak-check=full --show-leak-kinds=all ./mem_leak
(3)Valgrind 输出示例
plaintext
下载
==12345== Memcheck, a memory error detector ==12345== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==12345== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==12345== Command: ./mem_leak ==12345== Running memory leak example... Memory leaked! ==12345== ==12345== HEAP SUMMARY: ==12345== in use at exit: 40 bytes in 1 blocks ==12345== total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==12345== ==12345== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==12345== at 0x4C29F73: malloc (vg_replace_malloc.c:309) ==12345== by 0x40057E: leak_memory (mem_leak.c:6) ==12345== by 0x4005B2: main (mem_leak.c:17) ==12345== ==12345== LEAK SUMMARY: ==12345== definitely lost: 40 bytes in 1 blocks ==12345== indirectly lost: 0 bytes in 0 blocks ==12345== possibly lost: 0 bytes in 0 blocks ==12345== still reachable: 0 bytes in 0 blocks ==12345== suppressed: 0 bytes in 0 blocks ==12345== ==12345== For lists of detected and suppressed errors, rerun with: -s ==12345== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
实测代码
[oracle@localhost vscode]$ valgrind --leak-check=full --show-leak-kinds=all ./mem_leak
==41774== Memcheck, a memory error detector
==41774== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==41774== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==41774== Command: ./mem_leak
==41774==
Running memory leak example...
Memory leaked!
==41774==
==41774== HEAP SUMMARY:
==41774== in use at exit: 40 bytes in 1 blocks
==41774== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==41774==
==41774== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==41774== at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==41774== by 0x40058E: leak_memory (memory.c:5)
==41774== by 0x4005EB: main (memory.c:18)
==41774==
==41774== LEAK SUMMARY:
==41774== definitely lost: 40 bytes in 1 blocks
==41774== indirectly lost: 0 bytes in 0 blocks
==41774== possibly lost: 0 bytes in 0 blocks
==41774== still reachable: 0 bytes in 0 blocks
==41774== suppressed: 0 bytes in 0 blocks
==41774==
==41774== For lists of detected and suppressed errors, rerun with: -s
==41774== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
[oracle@localhost vscode]$
安装和调试信息
软件包 valgrind.x86_64.1.3.15.0-11.el7 将被 安装
正在尝试其它镜像。
http://mirrors.tuna.tsinghua.edu/centos/7.9.2009/updates/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
正在尝试其它镜像。
updates | 2.9 kB 00:00:00
Determining fastest mirrors
* base: mirrors.ustc.edu
* extras: mirrors.aliyun
* updates: mirrors.aliyun
正在解决依赖关系
--> 正在检查事务
---> 软件包 valgrind.x86_64.1.3.15.0-11.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
======================================================================================================================================
Package 架构 版本 源 大小
======================================================================================================================================
正在安装:
valgrind x86_64 1:3.15.0-11.el7 base 12 M
事务概要
======================================================================================================================================
安装 1 软件包
总下载量:12 M
安装大小:32 M
Is this ok [y/d/N]: y
Downloading packages:
valgrind-3.15.0-11.el7.x86_64. FAILED
http://mirrors.ustc.edu/centos/7.9.2009/os/x86_64/Packages/valgrind-3.15.0-11.el7.x86_64.rpm: [Errno 14] HTTP Error 404 - Not Found
正在尝试其它镜像。
valgrind-3.15.0-11.el7.x86_64. FAILED
http://mirrors.tuna.tsinghua.edu/centos/7.9.2009/os/x86_64/Packages/valgrind-3.15.0-11.el7.x86_64.rpm: [Errno 14] HTTP Error 404 - Not Found
正在尝试其它镜像。
valgrind-3.15.0-11.el7.x86_64.rpm | 12 MB 00:00:14
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : 1:valgrind-3.15.0-11.el7.x86_64 1/1
验证中 : 1:valgrind-3.15.0-11.el7.x86_64 1/1
已安装:
valgrind.x86_64 1:3.15.0-11.el7
完毕!
调试
[root@localhost vscode]# su oracle
[oracle@localhost vscode]$
[oracle@localhost vscode]$
[oracle@localhost vscode]$ vim memory.c
[oracle@localhost vscode]$
[oracle@localhost vscode]$ > memory.c
[oracle@localhost vscode]$
[oracle@localhost vscode]$ vim memory.c
[oracle@localhost vscode]$
[oracle@localhost vscode]$
[oracle@localhost vscode]$ gcc -o memory memory.c
memory.c: 在函数‘leak_memory’中:
memory.c:10:5: 错误:只允许在 C99 模式下使用‘for’循环初始化声明
for (int i = 0; i < 10; i++) {
^
memory.c:10:5: 附注:使用 -std=c99 或 -std=gnu99 来编译您的代码
[oracle@localhost vscode]$ gcc -std=c99 -o memory memory.c
[oracle@localhost vscode]$
[oracle@localhost vscode]$ ./memory
Running memory leak example...
Memory leaked!
[oracle@localhost vscode]$
[oracle@localhost vscode]$
[oracle@localhost vscode]$ gcc -g -o mem_leak memory.c # -g 生成调试信息
memory.c: 在函数‘leak_memory’中:
memory.c:10:5: 错误:只允许在 C99 模式下使用‘for’循环初始化声明
for (int i = 0; i < 10; i++) {
^
memory.c:10:5: 附注:使用 -std=c99 或 -std=gnu99 来编译您的代码
[oracle@localhost vscode]$
[oracle@localhost vscode]$ gcc -g -o mem_leak -std=c99 memory.c # -g 生成调试信息
[oracle@localhost vscode]$ ls
helloworld helloworld.c mem_leak memory memory.c
[oracle@localhost vscode]$
关键输出解析:
-
definitely lost: 40 bytes:明确泄漏了 40 字节内存。 -
调用栈:
-
泄漏发生在
leak_memory()函数的malloc调用(代码第 6 行)。 -
由
main()函数触发(代码第 17 行)。
-
3. 修复内存泄漏
在 leak_memory() 函数末尾添加 free(ptr):
c
复制
下载
void leak_memory() {
int *ptr = (int *)malloc(10 * sizeof(int));
// ...(其他代码不变)
free(ptr); // 修复泄漏
}
重新运行 Valgrind 后,输出应显示 All heap blocks were freed -- no leaks are possible。
4. Valgrind 常用参数
| 参数 | 作用 |
|---|---|
--leak-check=full | 显示泄漏的详细位置 |
--show-leak-kinds=all | 显示所有泄漏类型 |
--track-origins=yes | 追踪未初始化内存的来源 |
-s | 显示错误统计摘要 |
5. 总结
-
内存泄漏检测:Valgrind 能精确定位未释放的内存。
-
版本适配:Valgrind 3.15.0 完全支持 CentOS 7(需手动编译安装)。
-
扩展应用:还可检测越界访问、使用未初始化内存等问题(例如
valgrind --tool=memcheck ./program)。
📌 提示:生产环境中建议定期用 Valgrind 检查关键代码!
修改代码后再次实测 ok
// 忘记调用 free(ptr); 改成 free(ptr);// 忘记调用
gcc -g -o mem_leak -std=c99 memory.c # -g 生成调试信息
再次检测
valgrind --leak-check=full --show-leak-kinds=all ./mem_leak
日志代码如下
[oracle@localhost vscode]$ gcc -g -o mem_leak -std=c99 memory.c # -g 生成调试信息
[oracle@localhost vscode]$
[oracle@localhost vscode]$ valgrind --leak-check=full --show-leak-kinds=all ./mem_leak
==42512== Memcheck, a memory error detector
==42512== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==42512== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==42512== Command: ./mem_leak
==42512==
Running memory leak example...
Memory leaked!
==42512==
==42512== HEAP SUMMARY:
==42512== in use at exit: 0 bytes in 0 blocks
==42512== total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==42512==
==42512== All heap blocks were freed -- no leaks are possible
==42512==
==42512== For lists of detected and suppressed errors, rerun with: -s
==42512== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
[oracle@localhost vscode]$
版权声明:本文标题:centos下 内存泄露检测工具 Valgrind 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1766485546a3462766.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论