Skip to content Skip to sidebar Skip to footer

Clear R Memory Using Rpy2

I have a bunch of R functions which I need to call through python. However, I reach memory errors when I try to allocate a large matrix. The same functions run fine on RStudio on t

Solution 1:

To clean the R memory:

rm(list = ls())

Solution 2:

(...)

The same functions run fine on RStudio on the same computer.

May be the same function, but likely with differences in memory usage from other applications.

Your R function func2() returns the following object size:

> object.size(func2(1))
1053240200 bytes

This is about 1.05Gb.

The error I get is: RRuntimeError: Error: cannot allocate vector of size 1004.4 Mb

The error observed is likely occurring either because of what is happening in the unspecified function func1(), or because something has changed between the execution in RStudio and the execution in rpy2.


Solution 3:

I typically deal with the issue by allocating more memory

from rpy2 import robjects
R = robjects.r


R('memory.limit()')
R('memory.limit(size = 10000)') ## in MB
R('memory.limit()')

…
R('gc()')## trigger garbage collection

Post a Comment for "Clear R Memory Using Rpy2"