Skip to content

Unbounded memory use when specifying otypes='d' in vectorize. #11867

Closed

Description

@carlohamalainen

@nadiahpk first encountered this in the way that SciPy's hypergeom uses vectorize.

Here is a stand-alone example for reproducing the memory behaviour:

import numpy as np

class myclass():
    def f_class_method(self, k): return 0

    def __init__(self):
        self.f_class_method_vec_d = np.vectorize(self.f_class_method, otypes='d')
        self.f_class_method_vec   = np.vectorize(self.f_class_method)

# Memory use keeps growing:
def main1():
    while True:
        myclass().f_class_method_vec_d(0)

# Memory use constant:
def main2():
    while True:
        myclass().f_class_method_vec(0)

# Manual workaround for main1, now has constant memory use:
def main3():
    while True:
        m = myclass()
        m.f_class_method_vec_d(0)
        del m.f_class_method_vec_d._ufunc

I believe the issue is that self._ufunc becomes a circular reference when the function being vectorized is a class method.

I have a workaround here: carlohamalainen@9b5747b

Activity

  1. mattip commented on Sep 3, 2018

    @mattip
    Member

    Are you sure there is no leak with your fix? When I tried a test with sys.getrefcount() I find I need to activate the last 2 del statements to make it pass (CPython3 only, CPython2 passes without any deletes). Applying your fix does not change the situation for me. I am using the latest NumPy head.

        def test_leaks(self, dels):
            class A(object):
                def f(self, k):
                    return 0
    
                def __init__(self):
                    self.f_vec_otypes = np.vectorize(self.f, otypes='d')
                    self.f_vec = np.vectorize(self.f)
    
            a_tag = A()
            f_refcount = sys.getrefcount(A.f)
            for i in range(10):
                a = A()
                out = a.f_vec_otypes(0)
                out = a.f_vec(0)
                # del a.f_vec_otypes._ufunc
                # del a.f_vec._ufunc
                # del a.f_vec_otypes
                # del a.f_vec
            assert_equal(sys.getrefcount(A.f), f_refcount)
    

    Still playing with it to understand where the extra reference(s) is being created. Adding for i in range(10): gc.collect() influences the refcount number, but does not totally remove the need for the del statements.

  2. carlohamalainen commented on Sep 3, 2018

    @carlohamalainen
    Author

    You're right, the memory usage still grows, just more slowly.

    I added one del to my manual workaround and I see the memory usage (RES in htop) stay constant at 27528:

    def main3():
        while True:
            m = myclass()
            m.f_class_method_vec_d(0)
            del m.f_class_method_vec_d._ufunc
            del m.f_class_method_vec_d  # added this del
  3. mattip commented on Sep 7, 2018

    @mattip
    Member

    vectorize calls frompyfunc only in the actual function __call__. I wonder if that is what is creating references that even the GC cannot break.

  4. seberg commented on Jan 9, 2019

    @seberg
    Member

    Oops, forgot to add/keep a fixes to the commit. This was closed by gh-11977

  5. carlohamalainen commented on Jan 12, 2019

    @carlohamalainen
    Author

    Thanks @mattip and @seberg :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions