This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to free memory from unused meshes

0 votes

I am currently testing a geometry optimization algorithm that creates many meshes and I am running into memory issues. I suspect that the memory from unused meshes is not freed.
When I run the following script

from dolfin import *
import resource

def print_memuse():
     print str(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) + " kb"

def make_mesh():
     print_memuse()
     mesh = UnitSquareMesh(1000,1000)
     print_memuse()
     mesh = Mesh(mesh)
     print_memuse()

make_mesh()
print_memuse()

I get the result


81716 kb
160040 kb
238188 kb
238188 kb


It looks like even after the function make_mesh() has been called the memory is still in use. Has anyone had similar issues to this before?

asked Sep 30, 2015 by Gabriel Balaban FEniCS User (1,210 points)

1 Answer

+1 vote

When an executable takes memory from the OS, it seldom gives it back. What this means, is that if you run:

from dolfin import *
import resource

def print_memuse():
     print str(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) + " kb"

def make_mesh():
     print_memuse()
     mesh = UnitSquareMesh(1000,1000)
     print_memuse()
     mesh = Mesh(mesh)
     print_memuse()

make_mesh()
make_mesh()
make_mesh()
make_mesh()
print_memuse()

You will see:

96664 kb
169936 kb
240336 kb
240336 kb
240336 kb
240356 kb
240356 kb
240356 kb
240356 kb
240356 kb
240356 kb
240356 kb
240356 kb

so the system memory usage stays at the maximum it ever reached.

In summary, the memory from a deallocated mesh is released to the program, but not to the OS.

answered Oct 1, 2015 by chris_richardson FEniCS Expert (31,740 points)
...