← Back to team overview

dolfin team mailing list archive

[Bug 521507] Re: Memory leak in Python Expression interface

 

If you place

  return std::make_pair(0, true);

just after

  // Compute F(u)
  nonlinear_problem.form(*A, *b, x);
  nonlinear_problem.F(*b, x);

in NewtonSolver.cpp, the below script leaks so badly that your computer
will crash if you don't kill it pretty quick.


from dolfin import *

parameters["linear_algebra_backend"] = "uBLAS"

class MyProblem(NonlinearProblem):
    def __init__(self, a, L):
        NonlinearProblem.__init__(self)
        self.L = L
        self.a = a
        #self.L = Form(L)
        #self.a = Form(a)
    def F(self, b, x):
        return
    def J(self, A, x):
        return

mesh = UnitSquare(16, 16, "crossed")
V    = FunctionSpace(mesh, "Discontinuous Lagrange", 0)
v    = TestFunction(V)
du   = TrialFunction(V)
u    = Function(V)
u0   = Function(V)

for level0 in xrange(10000):
    for level in xrange(10):

        L = inner(v, u-u0)*dx
        a = derivative(L, u, du)

        # This leaks like crazy
        problem = MyProblem(a, L)
        solver  = NewtonSolver()
        solver.solve(problem, u.vector())

        # This does not leak
        #problem = VariationalProblem(a, L, [], nonlinear=True)
        #problem.solve(u)
    print "End inner loop"

-- 
Memory leak in Python Expression interface
https://bugs.launchpad.net/bugs/521507
You received this bug notification because you are a member of DOLFIN
Team, which is subscribed to DOLFIN.

Status in DOLFIN: Confirmed

Bug description:
The below code leads to a memory leak (I see a leak over about 2MB over 90s). The leak is related to the Expression. If I comment out the call to

    eval(Array<double>&, const Array<double>&);

from 

  dolfin::GenericFunction::evaluate(double*, const double*, const ufc::cell) const

I don't detect a leak.

------

from dolfin import *
parameters["form_compiler"]["cpp_optimize"] = True
parameters["form_compiler"]["optimize"] = True

class MyExpression(Expression):
    def eval(self, values, x):
        if x[0] < DOLFIN_EPS:
            values[0] =  1.0

mesh = UnitSquare(32, 32)
for t in xrange(20000):
    for level in xrange(100):
        V  = FunctionSpace(mesh, "CG", 1)
        v  = TestFunction(V)
        u  = Function(V)
        u0 = Function(V)
        f  = MyExpression(degree=1)
        L = v*(u-u0)*dx + v*f*ds
        b = assemble(L)
    print "----"





References