问题描述
以下最小示例似乎遭受内存泄漏(使用SciPy verison 0.17.0测试)
The following minimal example appears to suffer from memory leak (tested using SciPy verison 0.17.0)
import resource
from scipy.stats import rv_continuous
class Rv(rv_continuous):
def __init__(self, x):
rv_continuous.__init__(self, a=0, b=1)
self.x = x
def _pdf(self, y):
return 1
def call_rv(x):
rv = Rv(x)
# if the line below is commented out, memory usage stays constant
s = rv.rvs()
return 1
for k in range(10000):
x = call_rv(k)
if k%1000==0:
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print 'Memory usage: %s (kb)' % mem
在我的示例中,我不了解是什么导致了泄漏.值得注意的是,当注释掉随机变量生成s = rv.rvs()
时,不会发生泄漏.
I don't understand what causes the leak in my example. Notably, when the random variate generation s = rv.rvs()
is commented out, the leak doesn't occur.
使用rv_continuous
和随机变量生成时如何避免内存泄漏?
How can the memory leak be avoided when using rv_continuous
and random variate generation?
推荐答案
这不是内存泄漏,最终将把内存返回给操作系统.
This is not a memory leak, the memory is going to be returned to the OS, eventually.
rv = Rv(x)
在循环中创建一个新实例.请勿这样做,您的内存消耗将得到控制.如果要生成N个变量,请创建一次实例,然后执行.rvs(size=N)
.
creates a new instance in the loop. Don't do that, and your memory consumption will be in check.If you want to generate N variates, create the instance once and then do .rvs(size=N)
.
这篇关于使用rv_continuous生成随机变量时,如何解决scipy.stats中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!