Created
August 27, 2014 17:51
-
-
Save MarcCote/11cf991d94a5076ccbc4 to your computer and use it in GitHub Desktop.
Memory leak with vector of memoryviews
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- 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