# Why does NDSolve and ParametricNDSolveValue show different memory manage in same loop?

This is a follow-up question about How to delete InterpolationFunction created by NDSolve/ParametricNDSolve?. In the previous question, J. M.♦ and Albert Retey showed $HistoryLength = 0 and Clear[] are uesful to release memory occupied by InterplatingFunction. But in the present question, I found a different memory manage between NDSolve and ParametricNDSolveValue. The code is follows: (*NDSolve*)$HistoryLength = 0;
mpl = 1/Sqrt[6.70837*10^-39];
gsT = 106.75;

Sup[ΛI_?NumericQ, ΓI_?NumericQ] :=
Module[{a, ρr, Trad, tf, s, t, result}, tf = 10/ΓI;
s = NDSolve[{a'[t] ==
a[t]*Sqrt[(8 π)/(3 mpl^2) (ρr[
t] + ΛI^4/
a[t]^3 Exp[-ΓI t])], ρr'[t] +
4*Sqrt[(8 π)/(3 mpl^2) (ρr[
t] + ΛI^4/
a[t]^3 Exp[-ΓI t])] ρr[
t] == ΓI ΛI^4/
a[t]^3 Exp[-ΓI t],
a[0] == 1, ρr[0] == 0}, {a, ρr}, {t, 0, tf}];
{a = a /. s[[1]], ρr = ρr /. s[[1]]};
result = ρr[tf]*a[tf];
Remove[t];
result]

I use more power clean function Remove[] to replace Clear[], and in the loop

Do[Sup[2.2, time], {time, 1, 10000, 0.1}]

showing a stable memory which is not increaing. But using ParametricNDSolveValue with the same equation and loop, it shows a increasing memory.

Do[(Module[{s, as},
s = ParametricNDSolveValue[{a'[t] ==
a[t]*Sqrt[(8 π)/(3 mpl^2) (ρr[
t] + ΛI^4/
a[t]^3 Exp[-ΓI t])], ρr'[t] +
4*Sqrt[(8 π)/(3 mpl^2) (ρr[
t] + ΛI^4/
a[t]^3 Exp[-ΓI t])] ρr[
t] == ΓI ΛI^4/
a[t]^3 Exp[-ΓI t],
a[0] == 1, ρr[0] == 0}, {a, ρr}, {t, 0,
tf}, {ΛI, ΓI, tf}];
as = s[2.2, time, time/10]; Remove[s]; Remove[as];]), {time, 1, 10000,  0.1}]

or

s = ParametricNDSolveValue[{a'[t] ==
a[t]*Sqrt[(8 π)/(3 mpl^2) (ρr[
t] + ΛI^4/
a[t]^3 Exp[-ΓI t])], ρr'[t] +
4*Sqrt[(8 π)/(3 mpl^2) (ρr[
t] + ΛI^4/
a[t]^3 Exp[-ΓI t])] ρr[
t] == ΓI ΛI^4/
a[t]^3 Exp[-ΓI t],
a[0] == 1, ρr[0] == 0}, {a, ρr}, {t, 0,
tf}, {ΛI, ΓI, tf}];
Do[(Module[{as}, as = s[2.2, time, time/10]; Remove[as];]), {time, 1,
10000, 0.1}]

So is there a explain?

• @belisarius has settled, thanks! And I have tried to change every global variables to local and remove it in second code, but it's not useful too. – sejabs Dec 3 '15 at 14:04
• Some (but not all) leakage can be prevented with Method -> {"ParametricCaching" -> None} – Dr. belisarius Dec 3 '15 at 14:51
• I tried the method Method -> {"ParametricCaching" -> None, "ParametricSensitivity" -> None} in the code, it's not useful although. In my opinion, the InterpolatingFunction created by ParametricNDSolveValue can be removed through attached variable. Obviously there is something wrong. Maybe I need to consult with Wolfram staff. – sejabs Dec 3 '15 at 15:30
• You could report it to the wolfram support. – user21 Dec 9 '15 at 17:17