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

3 views (last 30 days)
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
Cristina
Cristina on 13 Aug 2011
Thank Paulo, this is very strange, I will have to contact with the Matlab support. I am getting exactly this:
>> memory
Maximum possible array: 349 MB (3.658e+008 bytes) *
Memory available for all arrays: 1085 MB (1.138e+009 bytes) **
Memory used by MATLAB: 604 MB (6.333e+008 bytes)
Physical Memory (RAM): 3024 MB (3.171e+009 bytes)
* Limited by contiguous virtual address space available.
** Limited by virtual address space available.
>> for i=1:300
[a]=hist(1);
clear a;
end
>> clear i
>> memory
Maximum possible array: 349 MB (3.658e+008 bytes) *
Memory available for all arrays: 1085 MB (1.138e+009 bytes) **
Memory used by MATLAB: 610 MB (6.393e+008 bytes)
Physical Memory (RAM): 3024 MB (3.171e+009 bytes)
* Limited by contiguous virtual address space available.
** Limited by virtual address space available.
Walter Roberson
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?

Sign in to comment.

Accepted Answer

Jan
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
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.

Cristina
Cristina on 13 Aug 2011
The code I wrote above is just an example. I am not going Out of memory because of 6 MB. I have a code with thousend of loops, inside these loops I need to calcualte several histograms of different vectors. So 6 MB many many times are Gigabytes, and Out of memory. I just simplified the problem to show how hist is consuming a lof of memory, and I am unable to clear this memory, at the end I am running out of memory.
Does anyone know how to clear the memory used by the hist command?
  4 Comments
Oleg Komarov
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.
Cristina
Cristina on 23 Oct 2011
Dear Oleg,
At the end I solved the problem sustituting hist by histc. The comand histc does not consume so much memory. Thank you for your hint!

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!