Memory leak in time-dependant dynamic mesh

Hi, I want to implement adaptive mesh refinement for time-dependant problem. I need to create new mesh, fes and GridFunction in the time-loop.
But, it will cause memory leak. Following is a small test code:

from ngsolve import *
# import gc
# gc.enable()

def Setup():
    mesh = Mesh(unit_square.GenerateMesh(maxh=1))
    for i in range(8):
        mesh.Refine()

    fes = H1(mesh, order=1, autoupdate=True)
    phi_old = GridFunction(fes, autoupdate=True)

    phi_old.Set(k) # <-- turn on this line will cause memory leak

    del phi_old
    # gc.collect()

    print("mesh number = {} ".format(mesh.ne))

for k in range(1000):
    Setup()
    print("k = {} ".format(k))

As the iterations progress, even though the number of mesh elements used each time remains consistent, the memory usage of the program continues to increase. del phi_old dosen’t work.

I found a similar post in this forum, but it also cause memory leak.

Attachment:

memory_leak.py (468 Bytes)

thank you for the report, it’s fixed now in the development version,
Joachim

Thank you for fixing the bug. I need to use ngsxfem with ngsolve.
I install xfem with pip. However, the version of ngsolve that matches the latest version of xfem is not the development version. If I directly use pip install xfem==2.1.2406.dev3, pip will automatically install ngsolve-6.2.2405.post22.dev1. This version of ngsolve still has memory leak issues. Please help update the version of xfem on pypi.org to match the latest version of ngsolve.