8

I believe AndersonDarlingTest and probably all other tests are keeping a copy of data somewhere and not freeing it:

Quit[]; (*Must be run in different cell*)


$HistoryLength = 0;
memdata = {{MemoryInUse[], MaxMemoryUsed[]}}~Join~Table[
     data = 
      RandomVariate[
       MultinormalDistribution[{0, 0}, {{1, 1/2}, {1/2, 2}}], 1000000];
     dist = MultinormalDistribution[{ux, uy}, {{a, c}, {c, b}}];
     AndersonDarlingTest[data, 
      dist, {"FittedDistributionParameters", "PValue"}];
     Clear@data; Clear@dist;
     ClearSystemCache[];
     {MemoryInUse[], MaxMemoryUsed[]}
     , {10}]/1024.^2;

ListPlot[Transpose@memdata, Joined -> True, 
 PlotLegends -> Placed[{"MemoryInUse", "MaxMemoryUsed"}, Top], 
 AxesLabel -> {"Iteration", "Memory (MB)"}]

example of memory leak

Questions:

  • Am I missing something obvious or is this a bug? Just want to make sure before I report it...
  • Is there a work around? I've been spelunking with the excellent spelunking package, but so far no luck...

Heavy symbols gives some candidates, but clearing the symbols in the loop gives me various errors.

heavySymbols["Statistics`*`"]

{"Statistics`GoodnessOfFitTestingDump`iHypothesisTestFunction", 
 "Statistics`StableEstimationDump`iLogPDF17", 
 "Statistics`StableEstimationDump`iPDF05b1"}

Using MMA 9.0.1 on Win7 64 bit.

Ajasja
  • 13,634
  • 2
  • 46
  • 104

1 Answers1

3

iHypothesisTestFunction seems to grow each iteration. But I have no idea, how to fix it. If I clear the whole object the tests stop working.

Example of memory abuse

$HistoryLength = 0;

Clear[$globalProperties];
$globalProperties = {OwnValues, DownValues, SubValues, UpValues, 
   NValues, FormatValues, Options, DefaultValues, Attributes, 
   Messages};

ClearAll[getDefinitions];
SetAttributes[getDefinitions, HoldAllComplete];
getDefinitions[s_Symbol] := 
  Flatten@Through[
    Map[Function[prop, 
       Function[sym, prop[sym], HoldAll]], $globalProperties][
     Unevaluated[s]]];

ClearAll[symbolMemoryUsage];
symbolMemoryUsage[sname_String] := 
  ToExpression[sname, InputForm, 
   Function[s, ByteCount[getDefinitions[s]], HoldAllComplete]];

GetMem[] := {MemoryInUse[], MaxMemoryUsed[], 
   symbolMemoryUsage[
    "Statistics`GoodnessOfFitTestingDump`iHypothesisTestFunction"]};
memdata = {GetMem[]}~Join~
    Table[data = 
      RandomVariate[
       MultinormalDistribution[{0, 0}, {{1, 1/2}, {1/2, 2}}], 1000000];
     AndersonDarlingTest[data];
     Clear@data; Clear@dist;
     ClearSystemCache[];
     GetMem[], {10}]/1024.^2;

ListPlot[Transpose@memdata, Joined -> True, 
 PlotLegends -> 
  Placed[{"MemoryInUse", "MaxMemoryUsed", "iHypothesisTestFunction"}, 
   Top], AxesLabel -> {"Iteration", "Memory (MB)"}]

Aha, it seems iHypothesisTestFunction is doing some caching, and I have to selectively remove the downvalues...

Here is the fix. Don't have time to describe in detail how it works. (basically I'm just resetting the down values -- any suggestions welcome).

ClearAll@CleariHypothesisTestFunctionMemory;
CleariHypothesisTestFunctionMemory[] :=
  DownValues@
    Statistics`GoodnessOfFitTestingDump`iHypothesisTestFunction = 
   Uncompress@
    "1:eJztWktv4zYQtrOvPjZFu4cCfVwK9I1edlGgi/\
bkxnESYJ3Ekbd76SG0RNlEaFElqWT978sRrZclOxyt7UWBXgiTGc4M5/\
HNkMo3E3EVPux0OgqGV0zp8ABmH5vhKuG0TzlZ0CDsZounggeXRGsqI0v5oRmORBQwzcRy\
6Q8zeJpow4356vpEiCCiSl2EA6bH1CxG034yj6/Z6SIWekYVU7A8SCK/\
YJLp4z0zP8xGTYO+\
mUo2SYAoldtLtJgbOb7d8sQMS93UD446zHQQPoDNIKaqT59oUmf8syPjI8I5lWEH9j8yw5\
+cRDf3TFNhj80wJNqfjez8q6rAV2wiiVxcn477fxGeUMxJPTDqeBFT7ylsIvOY07E4Tzi3\
osAQHtXvy4sopx0UphP+TXhYlqZ+dGQUYIhjIpX6yZE4TFm7UgPxC/\
WdIzVxppw4mzQg2pk2pnP1vSPtXexMSqbOpDF1J3Xnqm7DxxkUHIl5LJIoOH4bS0ObB/\
QyRV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