Valgrind的理解字节分配堆总结增加? [英] Valgrind understanding bytes allocated increase in heap summary?

查看:283
本文介绍了Valgrind的理解字节分配堆总结增加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个分叉TCP服务器调试内存使用情况。我觉得我做的pretty好,我只是似乎无法找到在堆总结'号'分配的字节的信息。这个数字似乎是不断增加的不再是我的服务器上运行:

I have been looking at debugging the memory usage in a forking TCP server. I think I am doing pretty well, I just can't seem to find information on the 'bytes allocated' number in the 'heap summary'. This number seems to be ever increasing the longer my server runs:

==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的报告allocs和自由都是平等的,没有泄漏是可能的,我不相信分配的字节增加。

Although Valgrind reports allocs and free are equal and no leaks are possible I do not trust the allocated bytes increasing.

所以:如果分配的字节数不断增加是否意味着我必须从堆释放的地方,即使Valgrind的报告没有泄漏是可能的。

So : if the bytes allocated keeps increasing does this mean I have to deallocate from the heap somewhere even if Valgrind reports no leaks are possible?

谢谢!

编辑:
戈登贝利的回答和其他提示,我还是有点疲惫。写了这个小程序:

With Gordon Bailey's answer and the other tips I am still a bit weary. Wrote this little app:

/* 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的输出是:

When I run this the Valgrind output is:

==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

因此​​,它看起来像所有的内存堆中清除,并从我的应用程序的输出肯定是不同的。

So it looks like all memory is cleared on the heap and is definitely different from the output of my app.

推荐答案

Valgrind的的字节分配是你分配在进程的运行时的总字节数。

Valgrind's bytes allocated is the total number of bytes you've allocated over the runtime of the process.

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

If you compile and run this weird little test program:

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

int main()
{
   int i;

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

   return 0;
}

Valgrind的输出是:

Valgrind's output is:

==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)

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

It seems like the real question now is how child processes affect this.

(编辑证实我的想法)

这篇关于Valgrind的理解字节分配堆总结增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆