How can I clear objects (and the memory they occupy) created via rpy?
import rpy2.robjects as r
a = r.r('a = matrix(NA, 2000000, 50)')
del a #if I do this, there is no change in the amount of memory used
r.r('rm(list=(ls(all=TRUE)))') # Same here, the objects disappear, but the memory is still used
The unfortunate effect is that in my application, memory usage increases until there is not enough and then it crashes... From the rpy2 docs:
The object itself remains available, and protected from R’s garbage collection until foo is deleted from Python
but even doing:
import rpy2.robjects as r
a = r.r('a = matrix(NA, 2000000, 50)')
r.r.rm('a')
del a
r.r.gc()
does not free the memory used...
EDIT: rpy2 2.0, Win XP, R 2.12.0
There is a paragraph in the rpy docs hinting that you may need to run the Python garbage collector frequently when deleting or overwriting large objects:
I was able to force rpy2 to free that large matrix by running
gc.collect()
immediately after creating the matrix, and again just after deleting it and running R's internalgc()
function. Running it in a loop with a sleep -- usetop
to watch the memory usage increase / decrease.Running under Python 2.6 on Ubuntu 10.0.4 with python-rpy version 2.0.8 linked to R version 2.10.1. Hope this helps you make some progress: