简体   繁体   English

Valgrind 显示内存泄漏,现在怎么办? R: C() 扩展

[英]Valgrind shows memory leaks and now what? R: C() extension

I have written an external C function to be called by the R .C() function.我编写了一个外部 C 函数,由 R .C()函数调用。 In order to check that no memory leaks occur I make the next call using valgrind from Ubuntu.为了检查没有发生内存泄漏,我使用 Ubuntu 中的valgrind进行下一次调用。

R --debugger=valgrind --vanilla < Desktop/eraseme.R

And I get no errors.我没有收到任何错误。 But I do get the notification that I have a memory leak.但是我确实收到了内存泄漏的通知。

==16347== HEAP SUMMARY:
==16347==     in use at exit: 30,440,904 bytes in 13,097 blocks
==16347==   total heap usage: 29,644 allocs, 16,549 frees, 134,692,871 bytes allocated
==16347== 
==16347== LEAK SUMMARY:
==16347==    definitely lost: 120 bytes in 2 blocks
==16347==    indirectly lost: 480 bytes in 20 blocks
==16347==      possibly lost: 0 bytes in 0 blocks
==16347==    still reachable: 30,440,304 bytes in 13,075 blocks
==16347==         suppressed: 0 bytes in 0 blocks
==16347== Rerun with --leak-check=full to see details of leaked memory

However I do not know how to find where is the leaking coming from.但是我不知道如何找到泄漏的来源。 The output says that I should run it with the option --leak-check=full but that option is not available.输出说我应该使用选项--leak-check=full运行它,但该选项不可用。

How can I find where is the leaking comming from?我怎样才能找到泄漏的来源? which call do I have to make to Valgrind taking in consideration that I am running an R script?考虑到我正在运行 R 脚本,我必须给Valgrind打哪个电话?

Thank you for your time.感谢您的时间。

EDIT: When I say that the option is not available I mean that I only have this options when I look for them.编辑:当我说该选项不可用时,我的意思是我在寻找它们时只有这些选项。

--arch                --help                --min-vsize           --no-restore-history  --silent
--args                --interactive         --no-environ          --no-save             --slave
--debugger-args       --max-nsize           --no-init-file        --no-site-file        --vanilla
--encoding            --max-ppsize          --no-readline         --quiet               --verbose
--file                --max-vsize           --no-restore          --restore             --version
--gui                 --min-nsize           --no-restore-data     --save    

EDIT: so indeed R shows that memory leak even if the C() function is correct.编辑:所以即使C()函数是正确的,R 确实显示内存泄漏。

@MartinMorgan is right! @MartinMorgan是对的! Even if I run a very stupid script that only has 1+1 I still get the minimal memory leak that is described above. 即使我运行一个只有1+1的非常愚蠢的脚本,我仍然会得到上面描述的最小内存泄漏。

Just to update on this question after 7 years:只是在 7 年后更新这个问题:

Using valgrind in R is documented in the "Writing R extensions" documentation:在 R 中使用valgrind记录在“Writing R extensions”文档中:

https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Using-valgrind https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Using-valgrind

The command line syntax is quite easy and I have added logging of R and valgrind output into different files:命令行语法非常简单,我已将 R 和 valgrind 输出的日志记录添加到不同的文件中:

R -d "valgrind --tool=memcheck --leak-check=full" --vanilla < my_code.R > r_output.txt 2> valgrind.txt

For maximum valgrind reports you could use these options:对于最大的 valgrind 报告,您可以使用以下选项:

R -d "valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --track-origins=yes -s" --vanilla -f my_code.R > R_output.txt 2> valgrind.txt

Note: -d = --debugger :注意: -d = --debugger

> man R
...
      -d, --debugger=NAME
              Run R through debugger NAME

       --debugger-args=ARGS
              Pass ARGS as arguments to the debugger
...

See also: How to redirect Valgrind's output to a file?另请参阅: 如何将 Valgrind 的输出重定向到文件?

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

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