I’m trying to understand an apparent memory issue regarding a simulation I’m doing.
Here’s a simplified version of what I’m running:
using DifferentialEquations, Plots
function usode!(du,u,p,t)
C1 = 1.63e-9
RS = 790.0
C2 = 3.3e-9
C0 = 0.3e-9
L1 = 1e-3
L2 = 82e-3
CM = 4e-12
LM = 8.0
RM = 7500.0
VC1, VC2, VC0, VCM, IL1, IL2, ILM, Vp = u
fv, VAmag = p
VA = VAmag*sinpi(2*Vp)
du[1] = 1/C1*IL1
du[2] = -1/C2*IL1
du[3] = 1/C0*(IL1-IL2-ILM)
du[4] = 1/CM*ILM
du[5] = 1/L1*(VA-VC1-VC0-VC2-IL1*RS)
du[6] = 1/L2*VC0
du[7] = 1/LM*(VC0-VCM-ILM*RM)
du[8] = fv
end
mutable struct Controller
f::Float64
end
function(c::Controller)(integrator)
integrator.p[1] = c.f
if c.f < 30000.0
c.f += 15.0
end
println(integrator.t)
end
function sim()
p = [0.0, 100.0]
cb1 = PeriodicCallback(Controller(27000.0),0.005)
cbs = CallbackSet(cb1,)
u0 = [0.0,0.0,0.0,0.0, 0.0,0.0,0.0, 0.0]
tspan = [0.0, 1.1]
prob = ODEProblem(usode!,u0,tspan,p)
@time sol = solve(prob,Tsit5(), callback=cbs, reltol=1e-8, abstol=1e-8, maxiters=10_000_000)
sol
end
sol = sim()
Execution takes about 30 seconds on this computer (4/8 cores/threads, 16 MB RAM, Windows 10). Watching the Memory view in Task Manager along with print statements from the code I can see pauses which I presume are garbage collector runs. Seems to be doing what I want and happens to use nearly all my RAM.
However, if I want to run the simulation again after making a small change to the constants in function usode!, and executing the last line again (using Atom/Juno and Ctrl-Enter) the next run is slow at first, then painfully slow, then essentially grinds to a halt, but if you wait long enough and maybe close everything else on the PC, then have a drink, eventually it finishes (1432 seconds).
What I expected was that when I executed sol=sim()
that the giant solution structure pointed to by sol
would be released to be garbage collected, but it seems like that didn’t happen.
So, I killed the Julia session, started again, and this time, did a sol = nothing
after the first run to try to more explicitly let the system reclaim the memory. Watching in Task Manager, it’s clear that this only partly works, and now I’m doing the dishes while waiting to see the execution time. It is also just smashing on the SDD now. CPU just sputtering along, waiting on everyone else. 1108 seconds!
So what is keeping all that memory in use and what can I do to tell Julia (and maybe some part of Atom/Juno) to let go of it?