Skip to main content
Tweeted twitter.com/#!/StackMma/status/581764594798034944
Copied & pasted code with Clear, not ClearAll- fixed
Source Link

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is:

  mpl = 1/Sqrt[6.70837*10^-39];
  gsT = 106.75;
  Sup[ΛI_?NumericQ, ΓI_?NumericQ] := 
  Module[{a, ρr, Trad, tf, s, t},
     Clear[tf];ClearAll[tf]; Clear[Trad];ClearAll[Trad]; Clear[s];ClearAll[s];
     tf = 10/ΓI;
     Clear[a];ClearAll[a]; Clear[ρr];ClearAll[ρr];
     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}, MaxStepFraction -> 10^-5, MaxSteps -> 10^6];
    {a = a /. s[[1]], ρr = ρr /. s[[1]]};
    Trad[t_] := (30/(π^2 gsT) ρr[t])^(1/4);
    Trad[tf]*a[tf]]

(I then evaluate Sup[x,y] for a variety of x and y values; actually, my goal is to make a contour plot of it.)

As you can see, I'm explicitly clearing a[t] and Rho[t] before I call NDSolve; however, these don't appear to get deleted after the module evaluates. Looking at the global variables, I have a$1058, a$1186, and so on. This is causing my memory usage to spiral out of control.

The problem seems to be that DownValues are being assigned to these functions and so they're not getting cleared. (I have set $HistoryLength to zero.)

How can I get the module to really, truly clear these functions?

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is:

  mpl = 1/Sqrt[6.70837*10^-39];
  gsT = 106.75;
  Sup[ΛI_?NumericQ, ΓI_?NumericQ] := 
  Module[{a, ρr, Trad, tf, s, t},
     Clear[tf]; Clear[Trad]; Clear[s];
     tf = 10/ΓI;
     Clear[a]; Clear[ρr];
     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}, MaxStepFraction -> 10^-5, MaxSteps -> 10^6];
    {a = a /. s[[1]], ρr = ρr /. s[[1]]};
    Trad[t_] := (30/(π^2 gsT) ρr[t])^(1/4);
    Trad[tf]*a[tf]]

(I then evaluate Sup[x,y] for a variety of x and y values; actually, my goal is to make a contour plot of it.)

As you can see, I'm explicitly clearing a[t] and Rho[t] before I call NDSolve; however, these don't appear to get deleted after the module evaluates. Looking at the global variables, I have a$1058, a$1186, and so on. This is causing my memory usage to spiral out of control.

The problem seems to be that DownValues are being assigned to these functions and so they're not getting cleared. (I have set $HistoryLength to zero.)

How can I get the module to really, truly clear these functions?

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is:

  mpl = 1/Sqrt[6.70837*10^-39];
  gsT = 106.75;
  Sup[ΛI_?NumericQ, ΓI_?NumericQ] := 
  Module[{a, ρr, Trad, tf, s, t},
     ClearAll[tf]; ClearAll[Trad]; ClearAll[s];
     tf = 10/ΓI;
     ClearAll[a]; ClearAll[ρr];
     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}, MaxStepFraction -> 10^-5, MaxSteps -> 10^6];
    {a = a /. s[[1]], ρr = ρr /. s[[1]]};
    Trad[t_] := (30/(π^2 gsT) ρr[t])^(1/4);
    Trad[tf]*a[tf]]

(I then evaluate Sup[x,y] for a variety of x and y values; actually, my goal is to make a contour plot of it.)

As you can see, I'm explicitly clearing a[t] and Rho[t] before I call NDSolve; however, these don't appear to get deleted after the module evaluates. Looking at the global variables, I have a$1058, a$1186, and so on. This is causing my memory usage to spiral out of control.

The problem seems to be that DownValues are being assigned to these functions and so they're not getting cleared. (I have set $HistoryLength to zero.)

How can I get the module to really, truly clear these functions?

deleted 208 characters in body
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is:

  mpl = 1/Sqrt[6.70837*10^-39];
  gsT = 106.75;
  Sup[\[CapitalLambda]I_Sup[ΛI_?NumericQ, \[CapitalGamma]I_ΓI_?NumericQ] := 
  Module[{a, \[Rho]rρr, Trad, tf, s, t},
     Clear[tf]; Clear[Trad]; Clear[s];
     tf = 10/\[CapitalGamma]I;ΓI;
     Clear[a]; Clear[\[Rho]r];Clear[ρr];
     s = NDSolve[{a'[t] == a[t]*Sqrt[(8 \[Pi]π)/(3 mpl^2) (\[Rho]r[t]ρr[t] + \[CapitalLambda]I^4ΛI^4/a[t]^3 Exp[-\[CapitalGamma]IΓI t])], \[Rho]r'[t]ρr'[t] + 4*Sqrt[(8 \[Pi]π)/(3 mpl^2) (\[Rho]r[t]ρr[t] + \[CapitalLambda]I^4ΛI^4/a[t]^3 Exp[-\[CapitalGamma]IΓI t])] \[Rho]r[t]ρr[t] == \[CapitalGamma]IΓI \[CapitalLambda]I^4ΛI^4/a[t]^3 Exp[-\[CapitalGamma]IΓI t], a[0] == 1, \[Rho]r[0]ρr[0] == 0}, {a, \[Rho]rρr}, {t, 0, tf}, MaxStepFraction -> 10^-5, MaxSteps -> 10^6];
    {a = a /. s[[1]], \[Rho]rρr = \[Rho]rρr /. s[[1]]};
    Trad[t_] := (30/(\[Pi]^2π^2 gsT) \[Rho]r[t]ρr[t])^(1/4);
    Trad[tf]*a[tf]]

(I then evaluate Sup[x,y]Sup[x,y] for a variety of xx and yy values; actually, my goal is to make a contour plot of it.)

As you can see, I'm explicitly clearing a[t]a[t] and Rho[t]Rho[t] before I call the NDSolve;NDSolve; however, these don't appear to get deleted after the module evaluates. Looking at the global variables, I have a\$1058a$1058, a\$1186a$1186, and so on. This is causing my memory usage to spiral out of control.

The problem seems to be that DownValuesDownValues are being assigned to these functions and so they're not getting cleared. (I have set $HistoryLength$HistoryLength to zero.)

How can I get the module to really, truly clear these functions?

Thanks!

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is:

  mpl = 1/Sqrt[6.70837*10^-39];
  gsT = 106.75;
  Sup[\[CapitalLambda]I_?NumericQ, \[CapitalGamma]I_?NumericQ] := 
  Module[{a, \[Rho]r, Trad, tf, s, t},
     Clear[tf]; Clear[Trad]; Clear[s];
     tf = 10/\[CapitalGamma]I;
     Clear[a]; Clear[\[Rho]r];
     s = NDSolve[{a'[t] == a[t]*Sqrt[(8 \[Pi])/(3 mpl^2) (\[Rho]r[t] + \[CapitalLambda]I^4/a[t]^3 Exp[-\[CapitalGamma]I t])], \[Rho]r'[t] + 4*Sqrt[(8 \[Pi])/(3 mpl^2) (\[Rho]r[t] + \[CapitalLambda]I^4/a[t]^3 Exp[-\[CapitalGamma]I t])] \[Rho]r[t] == \[CapitalGamma]I \[CapitalLambda]I^4/a[t]^3 Exp[-\[CapitalGamma]I t], a[0] == 1, \[Rho]r[0] == 0}, {a, \[Rho]r}, {t, 0, tf}, MaxStepFraction -> 10^-5, MaxSteps -> 10^6];
    {a = a /. s[[1]], \[Rho]r = \[Rho]r /. s[[1]]};
    Trad[t_] := (30/(\[Pi]^2 gsT) \[Rho]r[t])^(1/4);
    Trad[tf]*a[tf]]

(I then evaluate Sup[x,y] for a variety of x and y values; actually, my goal is to make a contour plot of it.)

As you can see, I'm explicitly clearing a[t] and Rho[t] before I call the NDSolve; however, these don't appear to get deleted after the module evaluates. Looking at the global variables, I have a\$1058, a\$1186, and so on. This is causing my memory usage to spiral out of control.

The problem seems to be that DownValues are being assigned to these functions and so they're not getting cleared. (I have set $HistoryLength to zero.)

How can I get the module to really, truly clear these functions?

Thanks!

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is:

  mpl = 1/Sqrt[6.70837*10^-39];
  gsT = 106.75;
  Sup[ΛI_?NumericQ, ΓI_?NumericQ] := 
  Module[{a, ρr, Trad, tf, s, t},
     Clear[tf]; Clear[Trad]; Clear[s];
     tf = 10/ΓI;
     Clear[a]; Clear[ρr];
     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}, MaxStepFraction -> 10^-5, MaxSteps -> 10^6];
    {a = a /. s[[1]], ρr = ρr /. s[[1]]};
    Trad[t_] := (30/(π^2 gsT) ρr[t])^(1/4);
    Trad[tf]*a[tf]]

(I then evaluate Sup[x,y] for a variety of x and y values; actually, my goal is to make a contour plot of it.)

As you can see, I'm explicitly clearing a[t] and Rho[t] before I call NDSolve; however, these don't appear to get deleted after the module evaluates. Looking at the global variables, I have a$1058, a$1186, and so on. This is causing my memory usage to spiral out of control.

The problem seems to be that DownValues are being assigned to these functions and so they're not getting cleared. (I have set $HistoryLength to zero.)

How can I get the module to really, truly clear these functions?

Source Link

Memory Problem with Modules and NDSolve

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is:

  mpl = 1/Sqrt[6.70837*10^-39];
  gsT = 106.75;
  Sup[\[CapitalLambda]I_?NumericQ, \[CapitalGamma]I_?NumericQ] := 
  Module[{a, \[Rho]r, Trad, tf, s, t},
     Clear[tf]; Clear[Trad]; Clear[s];
     tf = 10/\[CapitalGamma]I;
     Clear[a]; Clear[\[Rho]r];
     s = NDSolve[{a'[t] == a[t]*Sqrt[(8 \[Pi])/(3 mpl^2) (\[Rho]r[t] + \[CapitalLambda]I^4/a[t]^3 Exp[-\[CapitalGamma]I t])], \[Rho]r'[t] + 4*Sqrt[(8 \[Pi])/(3 mpl^2) (\[Rho]r[t] + \[CapitalLambda]I^4/a[t]^3 Exp[-\[CapitalGamma]I t])] \[Rho]r[t] == \[CapitalGamma]I \[CapitalLambda]I^4/a[t]^3 Exp[-\[CapitalGamma]I t], a[0] == 1, \[Rho]r[0] == 0}, {a, \[Rho]r}, {t, 0, tf}, MaxStepFraction -> 10^-5, MaxSteps -> 10^6];
    {a = a /. s[[1]], \[Rho]r = \[Rho]r /. s[[1]]};
    Trad[t_] := (30/(\[Pi]^2 gsT) \[Rho]r[t])^(1/4);
    Trad[tf]*a[tf]]

(I then evaluate Sup[x,y] for a variety of x and y values; actually, my goal is to make a contour plot of it.)

As you can see, I'm explicitly clearing a[t] and Rho[t] before I call the NDSolve; however, these don't appear to get deleted after the module evaluates. Looking at the global variables, I have a\$1058, a\$1186, and so on. This is causing my memory usage to spiral out of control.

The problem seems to be that DownValues are being assigned to these functions and so they're not getting cleared. (I have set $HistoryLength to zero.)

How can I get the module to really, truly clear these functions?

Thanks!