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=0show any effect? – Yves Klett Nov 10 '14 at 14:56Share[]– Dr. belisarius Nov 10 '14 at 16:27MemoryInUse[]image = Import["ExampleData/CTengine.tiff", "Image3D"]MemoryInUse[]GaussianFilter[image, 1];MemoryInUse[]. For version 10, the three memory numbers were 24600632, 35537848, 124676320. Thus, my computer consumed even more memory than the result given by @g3kk0. Next, I save the file and ran it with version 9, obtaining 26650296, 39502464, 97985304. Finally, I recreated the file and ran it on version 9, obtaining 25525112, 45144176, 104086784. – bbgodfrey Nov 17 '14 at 19:30