Skip to content

Instantly share code, notes, and snippets.

@MarcCote
Created August 27, 2014 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcCote/11cf991d94a5076ccbc4 to your computer and use it in GitHub Desktop.
Save MarcCote/11cf991d94a5076ccbc4 to your computer and use it in GitHub Desktop.
Memory leak with vector of memoryviews
import numpy as np
import resource
import metric
def test_memory_leak():
NB_LOOPS = 20
NB_LINES = 10000
NB_POINTS = 100
lines = [np.empty((NB_POINTS, 3), dtype="float32")] * NB_LINES
# Test fix
ram_usages = np.zeros(NB_LOOPS)
for i in range(NB_LOOPS):
metric.fct_with_fix(lines)
ram_usages[i] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print (["{0:.2f}Mo".format(ram/1024.) for ram in np.diff(ram_usages)])
# Exposing memory leak
ram_usages = np.zeros(NB_LOOPS)
for i in range(NB_LOOPS):
metric.fct_with_leaks(lines)
ram_usages[i] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print (["{0:.2f}Mo".format(ram/1024.) for ram in np.diff(ram_usages)])
test_memory_leak()
from libcpp.vector cimport vector
ctypedef float[:,:] Line
ctypedef vector[Line] Lines
cdef extern from *:
void __PYX_XDEC_MEMVIEW(Line* memslice, int have_gil)
cpdef fct_with_leaks(Lines lines):
pass
cpdef fct_with_fix(Lines lines):
for i in range(lines.size()):
__PYX_XDEC_MEMVIEW(&lines[i], 1)
# -- setup.py --
import numpy as np
from setuptools import setup
from Cython.Distutils import build_ext
from numpy.distutils.extension import Extension
setup(
cmdclass={'build_ext': build_ext},
ext_modules=[Extension("metric",
["metric.pyx"],
language='c++',
include_dirs=[np.get_include()])
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment