Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Free memory is lost after calling a function

Tags:

memory

matlab

I have some troubles with memory management in Matlab. Finally it leads to not enough free memory and an error.I tried to pinpoint the problem and found one interesting "feature": Somehow I loose free Memory in Matlab.

I do the following:
1) Start Matlab
2) typing "memory" I get: Maximum possible array: 1293 mb, Memory available for all arrays: 1456 mb
3) I'll call a function. The function is rather long, so it's hard to paste it here. But basically it loads 5 ca. 300mb mat files (sequentially), picks some few values and returns them. The returned matrix is ca. 1,2mb (4650x35 double)
4) I clear all variables in workspace ("clear all")
5) typing "memory" I get: Maximum possible array: 759 mb, Memory available for all arrays: 1029 mb

If I repeat steps 3) to 5) the memory numbers are constant.

So what is wrong here? Where do I loose the 400mb of free space? The memory used by Matlab is constant at around 330mb.

Does anyone have some ideas what is wrong here? Or is this something totally natural, but I miss it??

Thanks
Thomas

Ps: I use Matlab 2010a and Win 7 pro 32bit.

like image 931
Thomas Avatar asked Jul 21 '10 14:07

Thomas


1 Answers

A good part of this "lost" memory is probably due to memory fragmentation. As Matlab allocates and frees arrays over the course of a session, the memory gets broken up into smaller areas, and some is lost to overhead in the memory manager, at both the Matlab and the underlying C levels. The overhead is not counted as "used" by Matlab because it's not being used to hold M-code array values. Some memory may also be consumed by Matlab loading additional M-files and libraries, allocating internal buffers or structures, or by expansion of the Java heap in Matlab's embedded JVM. This is normal. After doing some work, Matlab won't have as much memory available as it did in a fresh session.

AFAIK, once low-level fragmentation occurs, there's nothing you can do to eliminate it aside from restarting Matlab. Allocating lots of small arrays can accelerate fragmentation. This sometimes happens if you use large cellstrs or large arrays of objects. So if you are having problems, you may need to reduce your peak memory usage in the function by breaking the work in to smaller chunks, reducing cell usage, and so on. And if you have big cellstr arrays in the MAT files, convert them to char. The "high water mark" of allocation is what governs fragmentation, so if you can break your data set in to smaller chunks, you can fit it in less memory.

Inside your function, clear as much as you can from one MAT file before moving on to the next. One way to do this implicitly is to move the per-file processing into a subfunction if it's currently sitting in a loop in your main function.

To help debug, do a "dbstop if all error", which will get triggered by the OOM. From there, you can use whos and the debugger to find out where the space is being taken up when you exhaust memory. That might reveal temp variables that need to be cleaned up, or suggest ways of chunking the work.

If you'd like to experiment to see what fragmentation looks like and how it affects memory()'s output, here's a function that will just create some fragmentation.

function fragmem(nbytes, chunksize)
%FRAGMEM Fragment the Matlab session's memory
if nargin < 2; chunksize = 1*2^10; end

nbytes = nbytes - rem(nbytes, chunksize);

nsteps = 100; % to make initial input relatively small
c = cell([1 nsteps]);
stepsize = nbytes / nsteps;
chunksperstep = ceil(stepsize / chunksize);
fprintf('Fragmenting %d MB memory into %d KB chunks (%d steps of %d chunks)\n',...
    round(nbytes/2^20), round(chunksize/2^10), nsteps, chunksperstep);

x = zeros([1 chunksperstep * chunksize], 'uint8');
colsizes = repmat(chunksize, [1 chunksperstep]);
for i = 1:nsteps
    c{i} = mat2cell(x, 1, colsizes);
end

Fragging 300 MB in 1KB chunks on my machine reproduces a "loss" on my win32 machine about the size you're seeing.

>> memory
Maximum possible array:            1384 MB (1.451e+009 bytes) *
Memory available for all arrays:   1552 MB (1.627e+009 bytes) **
Memory used by MATLAB:              235 MB (2.463e+008 bytes)
Physical Memory (RAM):             3311 MB (3.472e+009 bytes)

>> fragmem(300*2^20)
Fragmenting 300 MB memory into 1 KB chunks (100 steps of 3072 chunks)
>> memory
Maximum possible array:            1009 MB (1.059e+009 bytes) *
Memory available for all arrays:   1175 MB (1.232e+009 bytes) **
Memory used by MATLAB:              257 MB (2.691e+008 bytes)
Physical Memory (RAM):             3311 MB (3.472e+009 bytes)

>> 
like image 152
Andrew Janke Avatar answered Oct 10 '22 06:10

Andrew Janke