【问题标题】:Memory leak in minimal D program?最小 D 程序中的内存泄漏?
【发布时间】:2013-02-10 20:22:42
【问题描述】:

这个最小的程序:

int main()
{
  return 0;
}

使用 gcc 编译并使用 valgrind 运行时效果很好。

当使用

编译为 D 程序时
dmd test_new.d && valgrind ./test_new

我明白了:

HEAP SUMMARY:
    in use at exit: 360 bytes in 4 blocks
  total heap usage: 22 allocs, 18 frees, 52,024 bytes allocated

LEAK SUMMARY:
   definitely lost: 288 bytes in 3 blocks
   indirectly lost: 0 bytes in 0 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 72 bytes in 1 blocks
        suppressed: 0 bytes in 0 blocks

使用 gdc:

gdc -o test_new test_new.d && valgrind ./test_new

我明白了

HEAP SUMMARY:
    in use at exit: 568 bytes in 4 blocks
  total heap usage: 23 allocs, 19 frees, 52,664 bytes allocated

LEAK SUMMARY:
   definitely lost: 496 bytes in 3 blocks
   indirectly lost: 0 bytes in 0 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 72 bytes in 1 blocks
        suppressed: 0 bytes in 0 blocks

这里有什么问题?

【问题讨论】:

    标签: memory-leaks valgrind d


    【解决方案1】:

    我的猜测是 D 的运行时不会费心释放它的一些内存,但我不知道。 glibc 特别不会在程序关闭时释放一些东西,因为理论上操作系统将在程序结束后立即回收它。 D 的运行时可能会做同样的事情——尽管在 glibc 的情况下,它们有一个函数应该在你用 valgrind 运行程序时释放这些东西,以免 valgrind 报告错误。或者 D 的运行时可能只是有一个彻底的内存泄漏。必须追查才能确定。

    【讨论】:

    【解决方案2】:

    如果您在 valgrind 中使用 --leak-check=full 选项,它会告诉您泄漏的位置。

    我明白了

        ==32630== HEAP SUMMARY:
        ==32630==     in use at exit: 136 bytes in 4 blocks
        ==32630==   total heap usage: 16 allocs, 12 frees, 49,992 bytes allocated
        ==32630== 
        ==32630== 40 bytes in 1 blocks are definitely lost in loss record 4 of 4
        ==32630==    at 0x4026A68: calloc (vg_replace_malloc.c:566)
        ==32630==    by 0x8051C93: _d_monitor_create (in /home/dave/stack/test_new)
        ==32630==    by 0x8055B4F: thread_joinAll (in /home/dave/stack/test_new)
        ==32630==    by 0x804E47E: _D2rt6dmain24mainUiPPaZi6runAllMFZv (in /home/dave/stack/test_new)
        ==32630==    by 0x804E3F3: _D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv (in /home/dave/stack/test_new)
        ==32630==    by 0x804EA29: main (in /home/dave/stack/test_new)
        ==32630== 
        ==32630== LEAK SUMMARY:
        ==32630==    definitely lost: 40 bytes in 1 blocks
        ==32630==    indirectly lost: 0 bytes in 0 blocks
        ==32630==      possibly lost: 0 bytes in 0 blocks
        ==32630==    still reachable: 96 bytes in 3 blocks
        ==32630==         suppressed: 0 bytes in 0 blocks
    

    这似乎与线程和监视器的创建有关,请参阅mutex documentation

    【讨论】:

      【解决方案3】:

      看起来 D 的运行时正在造成内存泄漏(在 main 被调用之前/在 main 返回之后运行的东西)。您可以通过在ltracestrace 下运行程序来查看正在进行的活动,以查看是否有任何malloc 不是free'd 或者是否有任何mmap未映射的操作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-05
        • 2013-05-18
        • 2021-03-11
        • 2014-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多