5

I have written an external C function to be called by the R .C() function. In order to check that no memory leaks occur I make the next call using valgrind from Ubuntu.

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.

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?

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.

Usobi
  • 1,816
  • 4
  • 18
  • 25

2 Answers2

1

@MartinMorgan is right! Even if I run a very stupid script that only has 1+1 I still get the minimal memory leak that is described above.

Usobi
  • 1,816
  • 4
  • 18
  • 25
0

Just to update on this question after 7 years:

Using valgrind in R is documented in the "Writing R extensions" documentation:

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

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:

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

R Yoda
  • 8,358
  • 2
  • 50
  • 87