Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My R has memory leaks?

I'm using R 2.15.3 on Ubuntu 12.04 (precise) 64-bit. If I run R in valgrind:

R -d "valgrind" --vanilla

I then exit the program using q() and I get the following report:

==7167== HEAP SUMMARY:
==7167==     in use at exit: 28,239,464 bytes in 12,512 blocks
==7167==   total heap usage: 28,780 allocs, 16,268 frees, 46,316,337 bytes allocated
==7167== 
==7167== LEAK SUMMARY:
==7167==    definitely lost: 120 bytes in 2 blocks
==7167==    indirectly lost: 480 bytes in 20 blocks
==7167==      possibly lost: 0 bytes in 0 blocks
==7167==    still reachable: 28,238,864 bytes in 12,490 blocks
==7167==         suppressed: 0 bytes in 0 blocks
==7167== Rerun with --leak-check=full to see details of leaked memory
==7167== 
==7167== For counts of detected and suppressed errors, rerun with: -v
==7167== Use --track-origins=yes to see where uninitialised values come from
==7167== ERROR SUMMARY: 385 errors from 5 contexts (suppressed: 2 from 2)

Lately R is crashing quite often, especially when I call C++ functions through Rcpp, could this be the reason? Thanks!

like image 222
Matteo Fasiolo Avatar asked May 03 '13 12:05

Matteo Fasiolo


People also ask

How do I free up memory in R?

Using gc() function to remove all objects that are used from memory: gc() is used to remove all objects that are used from memory. reset is an optional parameter. It will return the maximum memory used in Mb.

Can memory leaks be fixed?

In most cases, you can fix the Windows 10 memory leak issues yourself. You can close resource-intensive apps, disable certain startup apps, and perform similar tasks to fix a memory leak.

Why is my r session using so much memory?

R uses more memory probably because of some copying of objects. Although these temporary copies get deleted, R still occupies the space. To give this memory back to the OS you can call the gc function. However, when the memory is needed, gc is called automatically.


1 Answers

You may be misreading the valgrind output. Most likely, there is no (obvious) leak here as R is pretty well studied as a system. Yet R is a dynamically typed language which has of course done allocations. "Definitely lost: 120 bytes" is essentially measurement error -- see the valgrind docs.

If you want to see a leak, create one, e.g., with a file like this:

library(Rcpp)
cppFunction('int leak(int N) {double *ptr = (double*) malloc(N*sizeof(double)); \
             return 0;}')
leak(10000)

which reserves memory, even explicitly out of R's reach, and then exits. Here we get:

$ R -d "valgrind" -f /tmp/leak.R
[...]
R> leak(10000)
[1] 0
R> 
==4479== 
==4479== HEAP SUMMARY:
==4479==     in use at exit: 35,612,126 bytes in 15,998 blocks
==4479==   total heap usage: 47,607 allocs, 31,609 frees, 176,941,927 bytes allocated
==4479== 
==4479== LEAK SUMMARY:
==4479==    definitely lost: 120 bytes in 2 blocks
==4479==    indirectly lost: 480 bytes in 20 blocks
==4479==      possibly lost: 0 bytes in 0 blocks
==4479==    still reachable: 35,611,526 bytes in 15,976 blocks
==4479==         suppressed: 0 bytes in 0 blocks
==4479== Rerun with --leak-check=full to see details of leaked memory
==4479== 
==4479== For counts of detected and suppressed errors, rerun with: -v
==4479== Use --track-origins=yes to see where uninitialised values come from
==4479== ERROR SUMMARY: 31 errors from 10 contexts (suppressed: 2 from 2)
$ 

Now there is a bit more of a leak (though it is still not as easily readable as one would hope). If you add the suggested flags, it will eventually point to the malloc() call we made.

Also, I have a worked example of actual leak in an earlier version of a CRAN package in one of my 'Intro to HPC with R' slide sets. If and when there is a leak, this helps. When there is none, it is harder to see through the noise.

So in short, if you code crashes, it is probably your code's fault. Try a minimal reproducible example is the (good) standard advice.

like image 162
Dirk Eddelbuettel Avatar answered Oct 09 '22 06:10

Dirk Eddelbuettel