pyamg: pyAMG leaking memory

Test script:

import numpy as np
import pyamg

stencil = [ [-1,-1,-1],[-1,8,-1],[-1,-1,-1] ]
A = pyamg.gallery.stencil_grid(stencil, (100,100), dtype=float, format='csr')
near_null_space = np.ones(A.shape[0])
ml = pyamg.smoothed_aggregation_solver(A, near_null_space[:, np.newaxis])

for _ in range(10000):
    rhs = np.random.randn(A.shape[0])
    x0 = np.random.randn(A.shape[0])
    solution = ml.solve(b=rhs.flatten(), x0=x0.flatten(), accel="bicgstab")

Running mprof python test.py and mprof plot reveals a steady rise of memory consumption:

memoryleak

Output of pip freeze:

appdirs==1.4.3
cycler==0.10.0
functools32==3.2.3.post2
matplotlib==2.0.2
memory-profiler==0.47
numpy==1.12.1
packaging==16.8
pkg-resources==0.0.0
psutil==5.2.2
py==1.4.33
pyamg==3.2.1
pyparsing==2.2.0
pytest==3.0.7
python-dateutil==2.6.0
pytz==2017.2
scipy==0.19.0
six==1.10.0
subprocess32==3.2.7

Output of python --version:

Python 2.7.12

I am running on Ubuntu 17.04.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (13 by maintainers)

Most upvoted comments

It may be the way we’re binding with SWIG. I’ll take a look. I’m also looking at using pybind11 going forward, which may help.

Thanks for this. I can reproduce in Python 3 as well. I’ve narrow the issue to relaxation. If I leave out pre/post smoothing then the memory stays bounded:

figure_1

In fact, just adding gauss_seidel to the loop results in the same thing:

figure_2

I’m using the following:

from memory_profiler import profile

@profile
def prof():
    import numpy as np
    import pyamg

    stencil = [ [-1,-1,-1],[-1,8,-1],[-1,-1,-1] ]
    n = 25
    A = pyamg.gallery.stencil_grid(stencil, (n,n), dtype=float, format='csr')
    ml = pyamg.smoothed_aggregation_solver(A, max_levels=3)

    b = np.random.randn(A.shape[0])
    x = np.zeros(A.shape[0])

    for i in range(50000):
        print(i)
        #solution = ml.solve(b)
        pyamg.relaxation.relaxation.gauss_seidel(A, x, b)

if __name__ == '__main__':
    prof()