import numpy as np import psutil, gc, os n = 100001 def _test_fill(z): arrays = [z+z, z+z] arrays3 = np.empty((len(arrays), len(arrays[0])), dtype='int64') for i in range(len(arrays)): row = arrays[i] for j in range(len(arrays[0])): arrays3[i,j] = row[j] pass def get_process(): pid = os.getpid() return psutil.Process(pid) _TWO_20 = float(2 ** 20) def get_lost(f): process = get_process() z = range(n) befor_mem = process.memory_info()[0] / _TWO_20 f(z) for i in range(20): gc.collect() after_mem = process.memory_info()[0] / _TWO_20 return after_mem - befor_mem def test_lost(): lost = get_lost(_test_fill) assert lost < 5, 'expected to lose less than 5, lost %d' % int(lost) if __name__ == '__main__': test_lost()