2014-12-02 83 views
2

繼續Python Memory Leak Using binascii, zlib, struct, and numpy但示例代碼正確說明了我有問題。Python內存泄漏結構和Numpy

import struct 
import zlib 
import binascii 
import numpy as np 
import os 
import psutil 
import gc 

l = list() 
l.append('eJztwYEAAAAAw6D5U1/gCFUB' + 'A'*161 + 'McA6vIAAQ==') 
l.append('eJztwYEAAAAAw6D5U1/hAFUB' + 'A'*3957 + 'HwGw6IAAQ==') 
l.append('eJztwYEAAAAAw6D5U1/hAFUB' + 'A'*3957 + 'LwGw6QAAQ==') 

process = psutil.Process(os.getpid()) 
for s in l: 
    print (process.get_memory_info()[0]/float(2 ** 20)) 
    byte_array = zlib.decompress(binascii.a2b_base64(s)) 
    array = np.array(struct.unpack('%dB' % (len(byte_array)), byte_array)) 
    del byte_array 
    del array 
    gc.collect() 
    print (process.get_memory_info()[0]/float(2 ** 20)) 

del l 
del s 
gc.collect() 
print (process.get_memory_info()[0]/float(2 ** 20)) 

它打印:

22.37109375 
25.83203125 
25.83203125 
95.65625 
95.65625 
166.69140625 
166.69140625 

爲什麼使用的內存不斷增加?爲什麼即使在變量被刪除之後腳本末尾仍會使用太多內存?謝謝。

回答

2

此鏈接http://bugs.python.org/issue14596是非常有幫助的。這個問題與結構模塊緩存格式字符串有關。如果我明確創建一個Struct對象,請使用它,然後將其刪除,問題消失。

import struct 
import zlib 
import binascii 
import os 
import psutil 
import gc 


def print_memory(string): 
    print string + str(process.get_memory_info()[0]/float(2 ** 20)) 

l = list() 
l.append('eJztwYEAAAAAw6D5U1/gCFUB' + 'A'*161 + 'McA6vIAAQ==') 
l.append('eJztwYEAAAAAw6D5U1/hAFUB' + 'A'*3957 + 'HwGw6IAAQ==') 
l.append('eJztwYEAAAAAw6D5U1/hAFUB' + 'A'*3957 + 'LwGw6QAAQ==') 

process = psutil.Process(os.getpid()) 
for s in l: 
    print_memory('Before inflating: ') 
    byte_array = zlib.decompress(binascii.a2b_base64(s)) 
    _struct = struct.Struct('%dB' % (len(byte_array))) 
    array = _struct.unpack(byte_array) 
    del byte_array 
    del array 
    del _struct 
    gc.collect() 
    print_memory('After inflating and deleting: ') 

del l 
del s 
gc.collect() 
print_memory('After deleting everything: ')