10
$\begingroup$

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

$\endgroup$
9
$\begingroup$

Please report this as a bug. A minimal example:

Quiet @ Remove["System`Dump`rep$*"];
	Quiet @ Remove["System`Dump`rules$*"];
Quiet @ Remove["IPOPTLink`Private`monitor$*"];

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

ToExpression[Names["System`Dump`rep$*"][[-1]], InputForm, DownValues]//Short

ToExpression[Names["System`Dump`rules$*"][[-1]], InputForm, OwnValues]//Short

ToExpression[Names["IPOPTLink`Private`monitor$*"][[-1]], InputForm, DownValues]//Short
{HoldPattern[System`Dump`rep$2694[System`Dump`m$_]]:>Module[{System`Dump`newm$},<<1>>]}

{HoldPattern[System`Dump`rules$2694]:>{Compiler:>{Compile::cif,<<28>>,CompiledFunction::cftec},<<7>>}}

{HoldPattern[IPOPTLink`Private`monitor$2659[{x14_}]]:>Null}

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

$\endgroup$
  • $\begingroup$ Thank you! I have submitted the bug report. $\endgroup$ – dzsoga Sep 26 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.