I am relatively new FEniCS user. I am using it to solve time dependent problem consisting of a system of 4 differential equations, that is Poisson equation and 3 drift-diffusion equations.

Plan is to extend it to at least 15 differential equations.

Currently, developed program works well and I managed to obtain good results (comparable to the commercial software). However, when I ran the program for a longer time, I have noticed that amount of used memory increases linearly with time. I tried to find the reason for this, so I reduced the code as much as possible, but the problem still remained. Next, I forced garbage collecting, which reduced memory usage a bit, however, it still kept increasing with each iteration.

In order to find the cause for this behavior, I tried to reduce the problem to the simplest case. I tried to run the textbook example i.e. Poisson equation with Dirichlet boundary condition over and over again for numerous iteration (order of million iterations) to check if memory usage increases in that case as well. I used psutil to track memory usage. The code is given below (modified and simplified version of example from the book).

```
from dolfin import *
import psutil
import os
pid = os.getpid()
memory_info = open('memory info.txt','w')
# Create mesh and define function space
mesh = UnitSquareMesh(6, 4)
V = FunctionSpace(mesh, "Lagrange", 1)
# Define boundary conditions
u0 = Expression("1 + x[0]*x[0] + 2*x[1]*x[1]", degree = 2)
def u0_boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, u0, u0_boundary)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = inner(nabla_grad(u), nabla_grad(v))*dx
L = f*v*dx
# Compute solution
u = Function(V)
for i in range(0,1000000):
solve(a == L, u, bc)
py = psutil.Process(pid)
process_memory = str(py.memory_info()[0]/2.0**30)
memory_info.write(str(i) + '\t'+ process_memory + '\n')
memory_info.flush()
```

I tested the code on 3 different machines and with 2 different versions of FEniCS:

- computing server with Xeon processor and FEniCS version 2018.1.0 on Xubuntu 18.04.2 installed from repository
- laptop with Core i7 8550u and FEniCS version 2019.1.0 on Xubuntu 18.04.2 installed from repository
- desktop computer with Core i5 3550 and FEniCS version 2019.1.0 on elementaryOS 5.0 installed from Anaconda.

In all three cases there was a memory leak and memory usage increases linearly with time (see figure below). Increase is actually small (for this case couple of MB per iteration), however for large number of iterations and equations, it accumulates rather quickly and becomes noticeable.

I repeated the calculations with different mesh size and some preliminary results show that it does not affect the rate of increase, just the starting memory. Also, by running code with mpirun I noticed that memory usage multiplies with number of used cores.

It is interesting that in a change log for FEniCS 2019.1.0 version, it is mentioned that memory leak is fixed.

Also, I found some bug report on a similar problem from last year that was caused by OpenMPI.

However, in my case memory leak happens even when I run code on a single core.

What can be the reason for this behavior and how to fix it? I would appreciate any information how to overcome this problem.