aaronjg
aaronjg

Reputation: 757

Figuring out maximum memory requirement for R function

How can I find the maximum memory requirement for an R function? I am trying to improve the resource requirements for a function, but am running into difficulty figuring out the maximum memory footprint.

It appears that gc() reports the maximum memory used, but this maximum is subject to issues of when gc is run during the function. The best I have been able to do is set a maximum memory with ulimit -v prior to starting R and running the script, and then decreasing that limit until the script fails. This is a rather slow, iterative process.

Is there a way to figure out the resource requirements in a single R session?

Upvotes: 2

Views: 99

Answers (1)

De Novo
De Novo

Reputation: 7620

Take a look at the documentation for profiling R code for memory use. The example in the help file for Rprofmem() is also helpful.

First call Rprofmem() with a file for the output and a lower limit for when to write the stack trace:

Rprofmem("Rprofmem.out", threshold = 1000)

Then run some code:

<your function>

Then turn off profiling and look at the file

Rprofmem(NULL)
noquote(readLines("Rprofmem.out", n = <some integer>))

The largest are at the top.

Upvotes: 2

Related Questions