Mistery: Hist command wastes a LOT of memory. Why?
3 views (last 30 days)
Show older comments
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?
7 Comments
Walter Roberson
on 13 Aug 2011
If you only do (say) 1 iteration, how does the Memory Used by MATLAB change? Is it possible that the memory used by MATLAB reflects the internal pcoding of hist and the routines it calls?
Accepted Answer
Jan
on 13 Aug 2011
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.
More Answers (2)
Walter Roberson
on 13 Aug 2011
Memory fragmentation, perhaps. It is a problem with systems that do dynamic memory allocation. I do not know what techniques MATLAB has adopted to reduce it.
0 Comments
Cristina
on 13 Aug 2011
4 Comments
Oleg Komarov
on 13 Aug 2011
Note that:
a = hist(1);
b = histc(1,-4:5);
Are the same, but hist calls histc and calculates the edges. Maybe you could avoid the loss if you call histc directly.
My terminology may have not been clear, counting/binning in hist/histc context is the same.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!