Sparse matrix memory usage clarification

9 views (last 30 days)
I am trying to understand the memory usage of a sparse matrix.
I have read the following:
Strategies for Efficient Use of Memory:
Sub-Topic : Make Arrays Sparse When Possible
Basic calculation for memory consumption :
In general, for a sparse double array with nnz nonzero elements and ncol columns, the memory required is
- 16 * nnz + 8 * ncol + 8 bytes (on a 64 bit machine)
- 12 * nnz + 4 * ncol + 4 bytes (on a 32 bit machine)
where
nnz = Number of non-zeros
ncol = Number of columns of the Sparse Matrix
I have a 64bit machine running 2019b.
I have the following code that does not follow the basic calculation for the above, namely, nnz does not seem to matter here.
For a sparse matrix of 6x6, after more than 1 element, the memory usage seem to remain stagnant at 232 bytes with increasing elements (until a dense enough matrix).
It seems that the calculation above does not match what I have obtained via whos for the memory of the sparse matrix.
Please advise on what I am missing.
Thank you!
a = sparse(6,6);
a(1,1)=1;
a(2,1) = 2;
a(1,2)=2;
a(2,2)=-1;
a(3,2)=2;
a(2,3)=2;
a(3,3)=-1;
a(4,3)=2;
a(3,4)=2;
a(4,4)=1;
a(6,6)=3;
whos("a")
Name Size Bytes Class Attributes
a 6x6 232 double sparse
a = sparse(6,6);
a(1,1)=1;
whos("a")
Name Size Bytes Class Attributes
a 6x6 72 double sparse
a = sparse(6,6);
a(1,1)=1;
a(2,1) = 2;
whos("a")
Name Size Bytes Class Attributes
a 6x6 232 double sparse

Accepted Answer

Bruno Luong
Bruno Luong on 14 Nov 2020
MATLAB allocates a larger memory than nnz for sparse matrix, it's call nzmax, with some strategy that is not documented. You should not count with nnz but with nzmax.
>> a=sparse(6,6);
>> a(1,1)=1
a =
(1,1) 1
>> whos a
Name Size Bytes Class Attributes
a 6x6 72 double sparse
>> nzmax(a)
ans =
1
>> a(2,1)=2
a =
(1,1) 1
(2,1) 2
>> whos a
Name Size Bytes Class Attributes
a 6x6 232 double sparse
>> nzmax(a)
ans =
11
>>
  1 Comment
Carl LEe
Carl LEe on 14 Nov 2020
Thank you! This helps me to somewhat compute the memory requirements of my large matrix, before execution.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 14 Nov 2020
Edited: James Tursa on 14 Nov 2020
Sparse matrices can be allocated with more than the minimum memory to hold the non-zero elements and their row indexes (see nzmax). When extra non-zero elements are inserted, they can use this extra memory in the background without increasing the sparse matrix memory footprint until the extra memory is used up, at which point a reallocation of memory is needed. The reallocation can be for more than one element in the background, giving the sparse matrix some new extra memory. The rules for how MATLAB does this in the background and how much extra memory will be allocated are not published. The memory formulas you see are generic and only refer to the minimum amount needed ... not necessarily the same as the actual amount allocated.
  1 Comment
Carl LEe
Carl LEe on 14 Nov 2020
Thanks for your prompt reply, and pointing me in the right direction.

Sign in to comment.

Categories

Find more on Sparse Matrices 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!