简体   繁体   English

valgrind将所有C操作视为“ malloc”和“ free”吗?

[英]What all C operations does valgrind treat as 'malloc' and 'free'?

At work I'm writing a rather complex piece of software in C, and I frequently test it using valgrind. 在工作中,我正在用C编写一个相当复杂的软件,我经常使用valgrind对其进行测试。 The program so far works perfectly with no memory leaks or array-bounds violations, and in the valgrind report, the number of 'frees' matched the number of 'mallocs' - great. 到目前为止,该程序运行良好,没有内存泄漏或违反数组边界的情况,在valgrind报告中,“ frees”的数量与“ mallocs”的数量匹配-很好。 The thing that bugs me is that it reports thousands of frees and mallocs. 让我感到困扰的是它报告了数千个免费和malloc。 And I know for a fact I'm not doing more than about 50-60. 而且我知道我做的不多于50-60。 I do know that when my program calls 'fopen', that call is counted by valgrind toward the number of mallocs, and similarly 'fclose' is counted toward the number of 'frees'. 我确实知道,当我的程序调用“ fopen”时,该调用由valgrind计入malloc的数量,类似地,“ fclose”也计入“ frees”的数量。 But in my case, this still doesn't explain the numbers I'm seeing for how many times memory is being malloced and freed. 但是就我而言,这仍然不能解释我看到的关于内存分配和释放多少次的数字。 I've scoured my code carefully looking for the culprit, but I got nothing. 我仔细检查了代码以寻找罪魁祸首,但一无所获。 I can't post any code here for obvious reasons, but I just want to know, am I missing something? 由于明显的原因,我无法在此处发布任何代码,但我只想知道,我是否缺少某些内容? Are there other C operations that valgrind counts toward the number of mallocs and frees? valgrind是否还有其他C操作计入malloc和free的数量?

Here's my valgrind report. 这是我的valgrind报告。 As you can see, everything looks good from this perspective. 如您所见,从这个角度来看,一切看起来都很不错。

Memcheck, a memory error detector
Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
Command: ./Codec
Parent PID: 3526

HEAP SUMMARY:
     in use at exit: 0 bytes in 0 blocks
   total heap usage: 2,407 allocs, 2,407 frees, 28,877,748 bytes allocated

 All heap blocks were freed -- no leaks are possible

 For counts of detected and suppressed errors, rerun with: -v
 ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

Well, if you call library functions that perform the malloc and free calls you will be seeing lot many allocations and frees. 好吧,如果您调用执行malloc和free调用的库函数,您将看到很多分配和释放。 some of the library functions, system calls that does allocations are strdup, pthread_create, timer_create, etc 一些库函数,进行分配的系统调用为strdup,pthread_create,timer_create等

Remember that there's many function calls that could allocate memory, strdup, fopen, creating threads, loading shared objects, parsing timezone or locale information (as a time related function could require), 3.d party libraries could allocate memory in a number of ways, and so on. 请记住,有很多函数调用可以分配内存,strdup,fopen,创建线程,加载共享对象,解析时区或语言环境信息(与时间相关的功能可能需要),3.d方库可以通过多种方式分配内存, 等等。

But, run your program with the valgrind massif tool, http://valgrind.org/docs/manual/ms-manual.html (read those docs) 但是,请使用valgrind massif工具( http://valgrind.org/docs/manual/ms-manual.html )运行程序(请阅读这些文档)

eg 例如

 valgrind --depth=20  --tool=massif ./Calc

This produces a massif.out.XXX file which shows you various sources of allocations, and the snapshot of the heap, eg as an excerpt: 这将生成一个massif.out.XXX文件,该文件向您显示各种分配来源以及堆的快照,例如作为摘录:

snapshot=9
#-----------
time=137984
mem_heap_B=640
mem_heap_extra_B=40
mem_stacks_B=0
heap_tree=peak
n2: 640 (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
 n1: 352 0x4BFD095A: __fopen_internal (in /usr/lib/libc-2.17.so)
  n1: 352 0x4BFD0A39: fopen@@GLIBC_2.1 (in /usr/lib/libc-2.17.so)
   n0: 352 0x8048784: main (tailq_example.c:63)
 n5: 288 0x8048580: add_block (tailq_example.c:20)
  n0: 72 0x8048748: main (tailq_example.c:60)
  n0: 72 0x804875C: main (tailq_example.c:61)
  n0: 72 0x80487DC: main (tailq_example.c:72)
  n0: 72 0x80487F0: main (tailq_example.c:73)
  n0: 0 in 1 place, below massif's threshold (01.00%)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM