spacepaste

  1.  
  2. import numpy as np
  3. import psutil, gc, os
  4. n = 100001
  5. def _test_fill(z):
  6. arrays = [z+z, z+z]
  7. arrays3 = np.empty((len(arrays), len(arrays[0])), dtype='int64')
  8. for i in range(len(arrays)):
  9. row = arrays[i]
  10. for j in range(len(arrays[0])):
  11. arrays3[i,j] = row[j]
  12. pass
  13. def get_process():
  14. pid = os.getpid()
  15. return psutil.Process(pid)
  16. _TWO_20 = float(2 ** 20)
  17. def get_lost(f):
  18. process = get_process()
  19. z = range(n)
  20. befor_mem = process.memory_info()[0] / _TWO_20
  21. f(z)
  22. for i in range(20):
  23. gc.collect()
  24. after_mem = process.memory_info()[0] / _TWO_20
  25. return after_mem - befor_mem
  26. def test_lost():
  27. lost = get_lost(_test_fill)
  28. assert lost < 5, 'expected to lose less than 5, lost %d' % int(lost)
  29. if __name__ == '__main__':
  30. test_lost()
  31.