# Memory leak with FindMaximum/Minimum inside of Module

This is my first question here. I have to solve coupled first order ODE-s and find the maximum of a function which depends on the solution of the ODE. I have to do this calculation in large quantities, so I wrote a function to do the job, but quickly ran out of memory. I think, this might be a minimal working example:

$HistoryLength = 0; ClearMemory := Module[{}, Unprotect[In, Out]; Clear[In, Out]; Protect[In, Out]; ClearSystemCache[];]; GetMemoryUse := UnitConvert[Quantity[MemoryInUse[], "Bytes"], "Megabytes"] // N; RunDE[ini_] := NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == ini}, y, {x, 0, 10}] sol = First@RunDE[1]; y2[x_] := y[x] /. sol Dummy[ini_] := Module[{x0 = ini, result}, result = FindMaximum[{y2[x], 5 < x < 10}, {x, 8}]; result[[1]] ] GetMemoryUse data = Dummy[#] & /@ Range[5 10^2]; ClearMemory Remove[data, sol] GetMemoryUse  It takes 5-10 seconds to compile, and shows the memory usage before and after using a Module with FindMaximum in a cycle. As you can see, I have already played with HistoryLength and Clear[In, Out]; Protect[In, Out]. The problem seems to be with the interpolating function. When I fed an explicit function to FindMaximum, I found no memory leak. How can this kind of memory leak resolved? Cheers, Zoltan ## 1 Answer Please report this as a bug. A minimal example: Quiet @ Remove["SystemDumprep$$*"]; Quiet @ Remove["SystemDumprules$$*"]; Quiet @ Remove["IPOPTLinkPrivatemonitor$*"];

if=NDSolveValue[{f'[x]==-2x+19,f[0]==4},f,{x,5,10}];
FindMaximum[{if[x], 5<x<10}, {x, 8}];

ToExpression[Names["SystemDumprep$*"][[-1]], InputForm, DownValues]//Short ToExpression[Names["SystemDumprules$*"][[-1]], InputForm, OwnValues]//Short

ToExpression[Names["IPOPTLinkPrivatemonitor$*"][[-1]], InputForm, DownValues]//Short  {HoldPattern[SystemDumprep$$2694[SystemDumpm$$_]]:>Module[{SystemDumpnewm$},<<1>>]}

{HoldPattern[SystemDumprules$2694]:>{Compiler:>{Compile::cif,<<28>>,CompiledFunction::cftec},<<7>>}} {HoldPattern[IPOPTLinkPrivatemonitor$2659[{x14_}]]:>Null}


All of these temporary variables should have been removed when the associated Module was exited.

• Thank you! I have submitted the bug report. – dzsoga Sep 26 '19 at 16:27