Search code examples
matlabmemorysparse-matrixallocation

Matlab - memory usage after solving a large system


I'm trying to calculate the memory used by a MATLAB process before and after solving a large sparse matrix. I'm using memory and the direct solver A\b.

What I want is just to mesaure by monitoring in some way the memory used by MATLAB and to calculate the difference between the memory used just after loading the file containing a sparse matrix and the memory used just after solving the sparse system.

Here the code I'm using

% load and store the sparse matrix into A
A = load('very_large_sparse_matrix.mat');

% store memory used after loading
usr = memory;
memory_after_load = usr.MemUsedMATLAB;

% solve the system
% no matter where b comes from
x = A\b

% store memory used after solving
usr = memory;
memory_after_solve = usr.MemUsedMATLAB;

% print the difference
disp(memory_after_solve - memory_after_load);

But the difference is always 0 or a negative integer. I think because MATLAB pre-allocates memory before running the code (am I wrong?) and it doesn't change the allocation dinamically if not for emergency.

I expect an increase of the memory used, because through direct solvers the fill-in increase the number of non-zero elements.

How can I calculate it? I have seen whos that gives the size in bytes of a variable, but what I'm looking for is the memory used by the process.

Thank you.

EDIT

I've just found that MATLAB pre-allocates its resources. Then an equivalent question could be is there a method to disable the pre-allocating system?


Solution

  • Thanks to @horchler, I've found a solution.

    Even if MATLAB pre-allocates all the memory it needs before the execution, spparams('spunomi', 3) shows the peaks inside the allocation.

    Also by doing [L,U,P,Q,R] = lu(A) and then calculating the difference between the number of nonzero elements in L and the number of nonzero elements in A, the function whos leads to the same results!