为什么用列表的numpy的阵列比较消耗这么多的内存? [英] Why does comparison of a numpy array with a list consume so much memory?

查看:720
本文介绍了为什么用列表的numpy的阵列比较消耗这么多的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点刺痛最近我。我通过从code列出去除numpy的阵列中所有的比较解决它。但是为什么垃圾收集想念收集呢?

This bit stung me recently. I solved it by removing all comparisons of numpy arrays with lists from the code. But why does the garbage collector miss to collect it?

运行这个,看它吃你的记忆:

Run this and watch it eat your memory:

import numpy as np
r = np.random.rand(2)   
l = []
while True:
    r == l

运行在64位的Ubuntu 10.04,virtualenv中1.7.2,2.7.3的Python,numpy的1.6.2

Running on 64bit Ubuntu 10.04, virtualenv 1.7.2, Python 2.7.3, Numpy 1.6.2

推荐答案

万一有人跌倒在此奇事......

Just in case someone stumbles on this and wonders...

@Dugal是的,我相信这是当前版本numpy的内存泄漏(9月2012)当一些异常被提出,发生(见的这个这个)。为什么要添加 GC 称之为@BiRico没有修复,似乎怪我,但必须appearently之后做什么?也许它与Python的垃圾如何收集回溯,如果有人知道的异常处理和垃圾colleciton CPython的内幕古怪,我会感兴趣。

@Dugal yes, I believe this is a memory leak in current numpy versions (Sept. 2012) that occurs when some Exceptions are raised (see this and this). Why adding the gc call that @BiRico did "fixes" it seems weird to me, though it must be done right after appearently? Maybe its an oddity with how python garbage collects tracebacks, if someone knows the Exception handling and garbage colleciton CPython Internals, I would be interested.

解决方法:这是不直接相关的名单,但例如大多数广播例外(空列表不适合于阵列大小,空数组产生相同的泄漏注意,在内部有永不表面的异常prepared)。因此,作为一种解决方法,你应该只检查第一如果形状是正确的(如果你这样做了很多,否则我就不会担心真的,如果我得到了它这个泄漏只是一个小串右)。

Workaround: This is not directly related to lists, but for example most broadcasting Exceptions (the empty list does not fit to the arrays size, an empty array results in the same leak. Note that internally there is an Exception prepared that never surfaces). So as a workaround, you should probably just check first if the shape is correct (if you do it a lot, otherwise I wouldn't worry really, this leaks just a small string if I got it right).

修正::此问题将与numpy的1.7

FIXED: This issue will be fixed with numpy 1.7.

这篇关于为什么用列表的numpy的阵列比较消耗这么多的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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