Valgrind理解分配的字节数增加堆总结?

时间:2012-07-30 21:55:54

标签: c valgrind

我一直在考虑调试分支TCP服务器中的内存使用情况。我觉得我做得很好,我似乎无法找到'堆汇总'中'字节分配'号码的信息。我的服务器运行的时间越长,这个数字似乎越来越大:

==27526== 
==27526== HEAP SUMMARY:
==27526==     in use at exit: 0 bytes in 0 blocks
==27526==   total heap usage: 113 allocs, 113 frees, 283,043 bytes allocated
==27526== 
==27526== All heap blocks were freed -- no leaks are possible
==27526== 
==27526== For counts of detected and suppressed errors, rerun with: -v
==27526== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
==27528== 
==27528== HEAP SUMMARY:
==27528==     in use at exit: 0 bytes in 0 blocks
==27528==   total heap usage: 120 allocs, 120 frees, 300,808 bytes allocated
==27528== 
==27528== All heap blocks were freed -- no leaks are possible
==27528== 
==27528== For counts of detected and suppressed errors, rerun with: -v
==27528== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
==27537== 
==27537== HEAP SUMMARY:
==27537==     in use at exit: 0 bytes in 0 blocks
==27537==   total heap usage: 127 allocs, 127 frees, 318,573 bytes allocated
==27537== 
==27537== All heap blocks were freed -- no leaks are possible
==27537== 
==27537== For counts of detected and suppressed errors, rerun with: -v
==27537== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

虽然Valgrind报告的alloc和free是相等的,并且没有泄漏是可能的,但我不相信分配的字节增加。

所以:如果分配的字节数不断增加,这是否意味着即使Valgrind报告没有泄漏,我也必须从堆中解除分配?

谢谢!

编辑: 有了Gordon Bailey的回答和其他提示,我仍然有点疲惫。写了这个小应用程序:

/* client.c */
#include <stdio.h>

void child_func(int childnum);

int main(int argc, char *argv[])
{
int nchildren = 1;
int pid;
int x;
if (argc > 1)
{
    nchildren = atoi(argv[1]);
}

for (x = 0; x < nchildren; x++)
{
    if ((pid = fork()) == 0)
    {
        child_func(x + 1);
        exit(0);
    }
}
wait(NULL);
return 0;
}

void child_func(int childnum)
{

int i;
for (i = 0; i < 1000; i++) {
            free(malloc(1));
    }
    sleep(1);
}

当我运行时,Valgrind输出为:

==28245== HEAP SUMMARY:
==28245==     in use at exit: 0 bytes in 0 blocks
==28245==   total heap usage: 1,000 allocs, 1,000 frees, 1,000 bytes allocated
==28245== 
==28245== All heap blocks were freed -- no leaks are possible
==28245== 
==28245== For counts of detected and suppressed errors, rerun with: -v
==28245== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
==28246== HEAP SUMMARY:
==28246==     in use at exit: 0 bytes in 0 blocks
==28246==   total heap usage: 1,000 allocs, 1,000 frees, 1,000 bytes allocated
==28246== 
==28246== All heap blocks were freed -- no leaks are possible

所以看起来所有内存都在堆上清除,并且肯定与我的应用程序的输出不同。

4 个答案:

答案 0 :(得分:3)

Valgrind的bytes allocated是您在流程运行时分配的总字节数。

如果你编译并运行这个奇怪的小测试程序:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int i;

   for(i = 0; i < 1000; ++i){
      free(malloc(1));
   }

   return 0;
}

Valgrind的输出是:

==2651== Memcheck, a memory error detector
==2651== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2651== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2651== Command: ./test_prog
==2651==
==2651==
==2651== HEAP SUMMARY:
==2651==     in use at exit: 0 bytes in 0 blocks
==2651==   total heap usage: 1,000 allocs, 1,000 frees, 1,000 bytes allocated
==2651==
==2651== All heap blocks were freed -- no leaks are possible
==2651==
==2651== For counts of detected and suppressed errors, rerun with: -v
==2651== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)

现在看来真正的问题是子进程如何影响这一点。

(编辑以确认我的想法)

答案 1 :(得分:0)

它只是所有malloc(和类似)调用的大小总和。如果你有自由,那个valgrind说你有,那就没问题了。即你的过程27526做了113个分配,总共283,043个字节,

如果你的服务器不断分配内存,那么这个数字会增加,调用free()不会减少这个数字。

答案 2 :(得分:0)

正如Godron所说,valgrind在当前进程中输出所有内存分配:

toc@UnixServer:~$ valgrind --leak-check=full ./pb_valgrind
==11411== Memcheck, a memory error detector
==11411== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==11411== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==11411== Command: ./pb_valgrind
==11411== 
==11414== 
==11414== HEAP SUMMARY:
==11414==     in use at exit: 0 bytes in 0 blocks
==11414==   total heap usage: 1,000 allocs, 1,000 frees, 1,000 bytes allocated
==11414== 
==11414== All heap blocks were freed -- no leaks are possible
==11414== 
==11414== For counts of detected and suppressed errors, rerun with: -v
==11414== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 6)
==11411== 
==11411== HEAP SUMMARY:
==11411==     in use at exit: 0 bytes in 0 blocks
==11411==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==11411== 
==11411== All heap blocks were freed -- no leaks are possible
==11411== 
==11411== For counts of detected and suppressed errors, rerun with: -v
==11411== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 6)

在你的程序中,你会做1000个大小为1字节然后1000空闲的malloc,它解释了输出。

问候。

答案 3 :(得分:0)

我认为问题在于,当我运行应用程序时,frees和allocs的数量会增加每个循环。这导致字节分配数量变得更大,因为每次alloc实际分配更多内存,因为有更多的实际alloc。

不确定为什么会这样,但它必须是我的错误代码,应该是另一个问题。

感谢您的帮助!