Probably this is a simple question, however I really don't understand this behavior in Mathematica. It particularly leads to a memory leak in a huge program I have to do processing of 3D images. I broke it down which took me quite a time and had to figure out that the problem occured in a simple function that includes a GaussianFilter
in Block
. The following simple code increases the memory each time I execute it and I can't figure out how to stop this:
image = Import["ExampleData/CTengine.tiff", "Image3D"];
oldMem = MemoryInUse[];
GaussianFilter[image, 1];
N[(MemoryInUse[] - oldMem)/1000/1000]
58.6569
Meaning that the consumed RAM increases by about 58 MB when the filter is applied even though nothing is returned. I checked for the data type that is produced by the GaussianFilter
. The data type changes from Byte
to Real32
. I know that Mathematica stores output internally but nothing is returned. Especially, I don't understand why the memory is never freed even if I put the stuff into scoping structures like Block
. By the way, setting $HistoryLength
to 0 doesn't help either.
ImageType@image
ImageType@GaussianFilter[image, 1]
Byte
Real32
fun[image_] := Block[
{},
GaussianFilter[image, 1];
];
oldMem = MemoryInUse[];
fun[image];
N[(MemoryInUse[] - oldMem)/1000/1000]
58.6571
$HistoryLength = 0;
oldMem = MemoryInUse[];
fun[image];
N[(MemoryInUse[] - oldMem)/1000/1000]
58.6571
The same problem also occurs when using Map
:
oldMem = MemoryInUse[];
$HistoryLength = 0;
Map[GaussianFilter[img3d, 1] &, Range[1, 10]];
oldMem/1000/1000 // N
MemoryInUse[]/1000/1000 // N
(MemoryInUse[] - oldMem)/1000/1000 // N
28.9377
620.817
591.878
Without saving anything into a variable, the memory has increased by 590 MB. Besides, setting $HistoryLength
to zero shows no effect. I also tried to Clear In
and Out
, again with no effect.
Unprotect[In, Out]
Clear[In, Out]
System specifictation
- OS: Windows 7 Professional, Service Pack 1 (64-bit)
- Processor: Intel Xeon CPU E5630 @ 2.53 GHz (2 Processors)
- Installed memory: 96 GB
- Mathematica 10.0.1
$HistoryLength=0
show any effect? $\endgroup$