Memory leak after multiple model.run()
I think this issue is similar to this old one: #439 (closed)
Summary
Executing several times model.run()
leads to a continuous increase in the memory usage. If one needs to optimize a parameter of a given setup according to the outcome of model.run()
, the user might run out of memory before the optimizer (e.g. scipy) stops.
Steps to reproduce
As a simple example, I took the model from the second notebook tutorial, with the LIGO design, and execute in a loop model.run():
import finesse, gc
import matplotlib.pyplot as plt
import numpy as np
from finesse.analysis.actions import Xaxis
finesse.init_plotting()
base = finesse.Model()
base.parse("""
l LI P=125
s s1 LI.p1 PRM.p1 L=0
# Power recycling mirror
m PRM T=0.03 L=37.5u phi=90
s prc PRM.p2 bs1.p1 L=53
# Central beamsplitter
bs bs1 R=0.5 T=0.5 phi=0 alpha=45
# X-arm
s sx bs1.p3 ITMX.p1 L=4.5
m ITMX T=0.014 L=37.5u phi=89.997
s Lx ITMX.p2 ETMX.p1 L=3995
m ETMX T=0 L=37.5u phi=89.997
# Y-arm
s sy bs1.p2 ITMY.p1 L=4.45
m ITMY T=0.014 L=37.5u phi=3m
s Ly ITMY.p2 ETMY.p1 L=3995
m ETMY T=0 L=37.5u phi=3m
# Signal recycling mirror
s src bs1.p4 SRM.p1 L=50.525
m SRM T=0.2 L=37.5u phi=-90
""")
# Computes the gravitational wave signal transfer function to the output of a detector.
# specifying our frequency range for future use
fstart = 5
fstop = 1e4
Npoints = 200
# This makes a copy of the model so we can add new commands to it
# without changing the original, which we will reuse
ifo = base.deepcopy()
ifo.parse(f"""
fsig(1)
sgen sig1 Ly.h 1 0
sgen sig2 Lx.h 1 180
pd1 TF SRM.p1.o f=fsig.f # with the additional mirror
""")
for _ in range(100): # just add more loops to crash your memory
out = ifo.run(Xaxis('fsig.f', 'log', fstart, fstop, Npoints))
# f, Hs = out.x1, np.abs(out['TF'])
gc.collect() # I tried this based on previous issues, but it doesn't seem to help
# plt.loglog(f, Hs)
# plt.show()
Edited by Carlos