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