清除rpy2使用的内存 [英] Clearing memory used by rpy2

查看:164
本文介绍了清除rpy2使用的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何清除通过rpy创建的对象(及其占用的内存)?

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

不幸的是,在我的应用程序中,内存使用量增加,直到内存不足为止,然后崩溃...来自rpy2

The unfortunate effect is that in my application, memory usage increases until there is not enough and then it crashes... From the rpy2 docs:

对象本身仍然可用, 并免受R的垃圾侵扰 收集,直到从中删除foo Python

The object itself remains available, and protected from R’s garbage collection until foo is deleted from Python

但即使这样做:

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

rpy2 2.0,Win XP,R 2.12.0

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:

R对象生活在R内存空间中,其大小是Python所不知道的,因此,当涉及到大对象时,Python似乎并不总是经常进行足够的垃圾回收.当大型对象被循环覆盖时,这有时会导致短暂的内存使用增加,尽管达到系统的内存限制似乎会触发垃圾回收,但人们可能希望显式触发垃圾回收.

R objects live in the R memory space, their size unbeknown to Python, and because of that it seems that Python does not always garbage collect often enough when large objects are involved. This is sometimes leading to transient increased memory usage when large objects are overwritten in loops, and although reaching a system’s memory limit appears to trigger garbage collection, one may wish to explicitly trigger the collection.

我能够通过在创建矩阵之后立即运行gc.collect(),并在删除矩阵并运行R的内部gc()函数之后再次运行rc2来释放大型矩阵.使其在睡眠中循环运行-使用top观察内存使用量的增加/减少.

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 internal gc() function. Running it in a loop with a sleep -- use top to watch the memory usage increase / decrease.

在Ubuntu 10.0.4上的Python 2.6下运行,并将python-rpy版本2.0.8链接到R版本2.10.1.希望这可以帮助您取得一些进步:

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:

import gc
import time

import rpy2.robjects as R

for i in range(5):
    print 'pass %d' % i
    R.r('a = matrix(NA, 1000000, 50)')
    gc.collect()
    R.r('rm(a)')
    R.r('gc()')
    gc.collect()

    print 'sleeping..'
    time.sleep(5)

这篇关于清除rpy2使用的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆