Sorting out memory leakage

Dear FreeFem users,

I am conducting a parameter study in which I call PEPSolve about a thousand times inside a double for loop. Oddly, as a computation proceeds, more and more RAM is used by the program, which is surprising as everything is scoped due to the for loop. Since apart from PEPSolve, several additional operations take place inside the loop, the code is structured using multiple macros and funcs. Although my 2D mesh is small (~10-20k triangles), due to the large number of calculations, the memory increase is substantial, and I would like to get rid of this behavior. I managed to reproduce this behavior with the attached code, which mimics the for loop + macro structure and also produces a slight increase in memory as a function of time.

How can I proceed with the investigation of the behavior? Can this happen due to incorrect use of func/macro usage (e.g., not every variable used inside a func/macro is an input)? Should one avoid defining variables inside the loop, and it is better to define them outside and just resize them inside the loop? Or maybe there are some pointers that are not deleted in the PETSc/SLEPc interface? Any help is appreciated.
Blasius-stability-1d-PEP-SLEPc-complex-memcheck.edp (6.3 KB)

You should use Valgrind and/or Massif to get a better insight.

Thanks for the tip. I checked out Valgrind memcheck and also Valgrind/Massif, but the output does not seem helpful for me since the FreeFem code is not referenced in the output (I assume that’s why there are a lot of ???-s in the file). I attach the outputs from the MWE above I sent you. Do you have a suggestion how to proceed?

vg.log (65.6 KB)
massif_graph.log (160.7 KB)

The Massif log does not report any substantial memory consumption increase. I’m running the script locally, now for 10 minutes, with no memory increase whatsoever. What tells you that there is such an increase?

Thanks for checking it out. In htop, I see that the memory consumption increases continuously. In my other scripts (which are 2D problems, not 1D, and a larger parameter range is swept, but the code is also long), this is so severe that the program runs out of memory eventually and stops prematurely.

I will check my large problem, run for a longer time, and see whether something is different.

I ran my other script. Massif output does not seem to detect a lot of memory consumption. However, htop tells a different story: as you can see in the screenshot, initially the memory consumption is ~2.5 GB, and it almost triples later. When I want to sweep a larger parameter space with my script, I can even run out of swap ram.

I suppose I can just move one of the for loops from the double for loop into a bash script, and call FreeFem multiple times, but this I find this behavior extremely frustrating. Do you have any further suggestions what I can do to further investigate this issue?

massif.3155.log (162.5 KB)

In your original script, there is a macro loopMacro which is then called inside a for loop. Could you write this for loop without the macro?

Yes I think it is possible, but then the code would look really messy, as there are actually multiple macros inside the macro loopMacro. Would it be better to use func instead of macro to structure the code, or should I just try putting the raw code inside the loop?

I think it would be worth not using either macro or func. There is no leak from the PETSc side of things, so I’m guessing it’s coming from FreeFEM, but I’m not really sure why.

Also maybe worth trying not running this as root.