Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
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. ) – xzczd Jan 24 '14 at 10:22
@xzczd I only see it with NMaximize. I ran your Do loop with no increase in memory use at all. (, MacOSXx86). – Ian Jan 24 '14 at 15:48
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[…… – xzczd Jan 25 '14 at 3:51
@xzczd Yeah ... tried with a fresh kernel. Thanks for thinking about this one! – Ian Feb 2 '14 at 19:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.