这是内存泄漏吗?(python + numpy)

Pie*_*e86 2 python memory-leaks numpy

以下代码填满了我的所有记忆:

from sys import getsizeof
import numpy

# from http://stackoverflow.com/a/2117379/272471
def getSize(array):
    return getsizeof(array) + len(array) * getsizeof(array[0])


class test():
    def __init__(self):
        pass
    def t(self):
        temp = numpy.zeros([200,100,100])
        A = numpy.zeros([200], dtype = numpy.float64)
        for i in range(200):
            A[i] = numpy.sum( temp[i].diagonal() ) 
        return A

a = test()
memory_usage("before")
c = [a.t() for i in range(100)]
del a
memory_usage("After")
print("Size of c:", float(getSize(c))/1000.0)
Run Code Online (Sandbox Code Playgroud)

输出是:

('>', 'before', 'memory:', 20588, 'KiB  ')
('>', 'After', 'memory:', 1583456, 'KiB  ')
('Size of c:', 8.92)
Run Code Online (Sandbox Code Playgroud)

如果c为~9 KiB,为什么我使用~1.5 GB的内存?这是内存泄漏吗?(谢谢)

memory_usage功能已发布在SO上,为清楚起见,此处报告:

def memory_usage(text = ''):
    """Memory usage of the current process in kilobytes."""
    status = None
    result = {'peak': 0, 'rss': 0}
    try:
        # This will only work on systems with a /proc file system
        # (like Linux).
        status = open('/proc/self/status')
        for line in status:
            parts = line.split()
            key = parts[0][2:-1].lower()
            if key in result:
                result[key] = int(parts[1])
    finally:
        if status is not None:
            status.close()
    print('>', text, 'memory:', result['rss'], 'KiB  ')
    return result['rss']
Run Code Online (Sandbox Code Playgroud)

Pie*_*e86 5

问题是由1.7.0版本的numpy的diagonal()函数引起的.升级到1.7.1解决了这个问题!

该解决方案由numpy邮件列表中的Sebastian Berg和Charles Harris提供.

  • 链接到实际修复内容的文档会很有帮助。 (2认同)