I'm using NMaximize on a function that calls NDSolve, and running out of memory. I created the following minimal example that (I think) recreates the problem I'm having, but on a smaller scale.

f[t_?NumericQ] := Block[{sol, s, x},
  sol = First[NDSolve[{x''[s] + Sin[x[s]] == 0, x[0] == 0, x'[0] == 1}, x, {s, 0, t}]];
  x[t] /. sol]

memInit = MaxMemoryUsed[];
  {f[t], t < Pi, t > 0}, t,
  Method -> "NelderMead",
  EvaluationMonitor :> Sow[MaxMemoryUsed[] - memInit]]][[2]]]

On my machine, with a freshly launched MathKernel, the memory usage increases badly after a run of not increasing at all. Here's the plot:

Memory usage after each evaluation of f

If I run NMaximize on Sin[t] instead of f[t], the memory usage does not increase. Can anyone explain the difference in behavior or suggest how to reduce memory usage?

  • $\begingroup$ Furthermore, NMaximize seems not to play a role in the memory consuming. (ListPlot@Reap[Do[f[t]; Sow[MaxMemoryUsed[] - memInit], {t, 100}]][[2, 1]] shows similar result. ) Then, let's wait for someone else to answer your question :D (The memory management behind NDSolve is so complicated that it's beyond my reach. ) $\endgroup$ – xzczd Jan 24 '14 at 10:22
  • $\begingroup$ @xzczd I only see it with NMaximize. I ran your Do loop with no increase in memory use at all. (, MacOSXx86). $\endgroup$ – Ian Jan 24 '14 at 15:48
  • $\begingroup$ Have you tried it with a fresh kernel? This happens when I start a fresh kernel and run the following 3 lines of code: f[x_?NumericQ]:=……; memInit=……; ListPlot[…… $\endgroup$ – xzczd Jan 25 '14 at 3:51
  • $\begingroup$ @xzczd Yeah ... tried with a fresh kernel. Thanks for thinking about this one! $\endgroup$ – Ian Feb 2 '14 at 19:44

Your Answer

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

Browse other questions tagged or ask your own question.