The memory leak in NIntegrate
is a bug and has been fixed as of version 10.2.0. Earlier versions would lose around 720 bytes per evaluation for this example, which could not be recovered without restarting the kernel.
ClearSystemCache[]
should be used to make sure the memory is released. Using version 10.2:
NI[z_?NumericQ, b0_?NumericQ] :=
NIntegrate[E^-Abs[y - z], {y, -b0, b0}];
NI[RandomReal[], .5]; (* NIntegrate autoloading *)
m1 = MemoryInUse[];
For[i = 1, i < 1000, i++; NI[RandomReal[], .5]; ClearSystemCache[]];
m2 = MemoryInUse[];
m2 - m1
(* -12744 *)
Also, if generating many outputs, it may be useful to set $HistoryLength = 0
or another small number.
We can evaluate the following to test whether the NIntegrate
call leaks any memory:
Block[{$HistoryLength = 0}, ClearSystemCache[];
Take[Table[before = MemoryInUse[];
NIntegrate[E^-Abs[y - RandomReal[]], {y, -.5, .5}];
ClearSystemCache[]; MemoryInUse[] - before, {10000}], -20]]
Version 10.2.0 shows no leak,
(* {0, 0, 0, 0, -64, 64, 0, 0, -64, 64, 0, 0, -64, 64, 0, 0, 0, 0, 0, 0} *)
while version 10.1.0 does
(* {656, 720, 656, 592, 720, ..., 656, 656, 656, 656, 656, 656} *)