Skip to content

Instantly share code, notes, and snippets.

@mattip
Created July 14, 2017 08:56
Show Gist options
  • Save mattip/cdfe4c6875fb0dc2cc56a283eb848127 to your computer and use it in GitHub Desktop.
Save mattip/cdfe4c6875fb0dc2cc56a283eb848127 to your computer and use it in GitHub Desktop.
memory "leak" in cpyext
running with "pypy test_memory 10" -> 77 MB retained when using range()
running with "cpython test_memory 10" -> 0.6 MB retained when using range()
import numpy as np
import psutil
import gc
import os, sys
try:
import __pypy__
except ImportError:
__pypy__ = None
def get_process():
pid = os.getpid()
return psutil.Process(pid)
_TWO_20 = float(2 ** 20)
def get_memory(process):
return process.memory_info()[0] / _TWO_20
def add_pressure(x):
if not __pypy__:
return
__pypy__.add_memory_pressure(x)
def test_memory(n):
proc = get_process()
start = get_memory(proc)
for i in range(n):
a = np.arange(100000)
del a
for i in range(10):
add_pressure(1000000)
gc.collect()
stop = get_memory(proc)
print 'arange retaining', stop - start, 'MB'
start = stop
for i in range(n):
a = np.array(range(100000))
del a
for i in range(10):
add_pressure(1000000)
gc.collect()
stop = get_memory(proc)
print 'range retaining', stop - start, 'MB'
assert stop - start < 100
if __name__ == '__main__':
if len(sys.argv) > 1:
n = int(sys.argv[1])
else:
n = 1
test_memory(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment