2
votes

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?

1
Interesting question. Looking forward to seeing what the answers are. - rayryeng
I don't think the issue is pre-allocation as much as it is Matlab returning the extra memory it requested from the System to the System after the solution is completed. I'd be aghast if there was an option to turn that mechanism off. You may be able to get a rough estimate by comparing the Peak Memory usage to the Current Memory usage from the System. - TroyHaskin
I thought about comparing manually System's information, too. I don't need a precise result then it coul be a good working (temporary) solution. - Firaja
It's not clear what you're actually looking for (or why). Do you want to fix the amount of memory allocated to Matlab? This question likely has more to do with application and OS level configuration, rather than Matlab-specific options. I'd imagine that SuperUser.com might be a better venue. - horchler
@horchler I do not want to fix anything. I just want to measure the amount of memory allocated due to the fill-in when a direct method is applied on a sparse matrix. - Firaja

1 Answers

0
votes

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!