MATLAB: Mistery: Hist command wastes a LOT of memory. Why

histmemory

I have a memory problem with the "hist" command. It uses a lot of memory, and I have no idea how to clear it. I need to clear this memory since I am going Out of memory.
You can see this example:
memory
for i=1:300
[a]=hist(1);
clear a;
end
clear i;
memory
The "Memory used by Matlab" after doing hist 300 times is around 6 MB more, even if I am clearing "a" every time. Where is this memory going? How can I clear it?

Best Answer

I do net get any differences in the memory usage under MATLAB 2009a and 6.5.
Does the problem concern HISTC also? Does running the loop 600 times change the lost memory - perhaps to 12MB? Then I'd assume, that the underlying Mex function histc has a memory leak. You can look into the source of hist.c, which is fortunately shipped with MATLAB.
But a comparison of hist.c between the versions 6.5 and 2009a shows, that only the int->mwSize/mwIndex/mwSignedIndex and some catching of unexpected cases has been modified. But perhaps 2008b contained further lines, which could potentially leak memory.
Related Question