Skip to content

Memory leak with ndarray as function arg in Cython #13237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ArtificialQualia opened this issue Apr 1, 2019 · 2 comments
Closed

Memory leak with ndarray as function arg in Cython #13237

ArtificialQualia opened this issue Apr 1, 2019 · 2 comments

Comments

@ArtificialQualia
Copy link

ArtificialQualia commented Apr 1, 2019

When using an ndarray as an arg to a cdef function, and that ndarray is not used in the function, that object is never properly garbage collected and causes a memory leak.

If the ndarray is used in the function, it is collected properly.

Reproducing code example:

memleak.pyx

import numpy as np
cimport numpy as cnp
from numpy cimport ndarray
cnp.import_array()

cdef inline void caller():
    cdef:
        ndarray[int] x
    x = np.asarray(range(10000))
    dont_use_x(x)

cdef inline void dont_use_x(ndarray[int] x):
    pass

while True:
    caller()

setup.py:

from distutils.core import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules = cythonize("memleak.pyx"),
    include_dirs=[numpy.get_include()]
)

to run:

python setup.py build_ext --inplace
python
import memleak

Numpy/Python version information:

1.16.2 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]

@mattip
Copy link
Member

mattip commented Jul 17, 2019

This seems like a cython issue. Can you raise it there?

@seberg
Copy link
Member

seberg commented Aug 9, 2019

Closing, since it is a cython issue as mentioned (and that issue has been created).

@seberg seberg closed this as completed Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants