我在代码中有一个类似于这个的小部分(当然,实际上使用的是真实矩阵而不是填充了零的矩阵):
x = [rinterface.FloatSexpVector([0]*(1000**2)) for i in xrange(20)]
y = robjects.r('list')(x)
看起来这可能会导致内存泄漏。
运行以下代码时:
for i in xrange(10):
x = [rinterface.FloatSexpVector([0]*(1000**2)) for i in xrange(20)]
y = robjects.r('list')(x)
del x
del y
robjects.r('gc(verbose=TRUE)')
I get:
Error: cannot allocate vector of size 7.6 Mb
In addition: Warning messages:
1: Reached total allocation of 2047Mb: see help(memory.size)
2: Reached total allocation of 2047Mb: see help(memory.size)
3: Reached total allocation of 2047Mb: see help(memory.size)
4: Reached total allocation of 2047Mb: see help(memory.size)
这是一个bug还是我需要进行其他操作?我也尝试过将变量命名为robjects.globalenv中的名称,然后在执行gc()之前对其进行rm()处理,但似乎不起作用。
我应该提一下,我正在windows上运行rpy 2.3dev,但在linux上也会出现这种情况,使用的是rpy 2.2.6(尽管由于linux运行64位版本而不是像windows机器一样运行32位版本,内存仅增长而不会出现2047mb错误)。
编辑: 在第一个代码示例之前添加gc.collect()似乎解决了问题,但这没有解决我的问题-深入研究我的代码后,我发现导致问题的那一行是赋值到.names,类似于这样:
x = [rinterface.FloatSexpVector([0]*(1000**2)) for i in xrange(20)]
y = robjects.r('list')(x)[0]
y.names = rinterface.StrSexpVector(['a']*len(y))
在清理之前放置rinterface.NULL也没有帮助。有什么建议吗?