Memory usage in sparse matrix
42 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, Sparse matrices store only the nonzero elements and their position indices. Using sparse matrices can significantly reduce the amount of memory required for data storage. But surprisingly I noticed that my computer runs out of memory when I try something like >>sparse(zeros(80000)) or a matrix having more elements.In zeros(), as there is no non-zero element, the sparse matrix should not have used any memory in this case at all! I would really appreciate if anyone can elucidate this issue... thanks.
2 Commenti
Matt J
il 19 Apr 2014
Modificato: Matt J
il 19 Apr 2014
as there is no non-zero element, the sparse matrix should not have used any memory in this case at all!
This wasn't your main problem, but it should be pointed out that sparse matrices can in fact consume a great deal of memory, even when containing only zeros. E.g., the following matrix which consumes over 200 MB
>> A=sparse(1,3e7); whos A
Name Size Bytes Class Attributes
A 1x30000000 240000024 double sparse
James Tursa
il 21 Apr 2014
Modificato: James Tursa
il 21 Apr 2014
@tanmoy: The minimum data storage requirement formula for a double m x n sparse matrix with nnz non-zero elements, including the index data, is as follows on a 32-bit system:
bytes = max(nnz,1) * (4 + 8) + (n+1)*4
Which breaks down as follows:
nnz * 4 = Storing the row index of the non-zero elements
nnz * 8 = Storing the non-zero double element values themselves
(n+1)*4 = Storing the cumulative number of non-zero elements thru column
So you see that even if there are no non-zero elements, the cumulative column index data still gets stored (hence the large memory for Matt J's example). As a user exercise, try sparse(3e7,1) instead.
For 64-bit systems using 8-byte integers for the indexing you can replace the 4's above with 8's.
This is just the minimum requirements. A sparse matrix can have excess memory allocated beyond the minimum if desired.
Risposta accettata
Ken Atwell
il 19 Apr 2014
Try just:
>> sparse(80000,80000)
to avoid the temporary allocation of a 80000^2 non-sparse zeros array.
2 Commenti
Ken Atwell
il 20 Apr 2014
See Geoff's comments below. While even an all-zeros sparse matrix will occupy some memory for book-keeping, it should be minimal in the grand scheme of things.
Più risposte (1)
Geoff Hayes
il 19 Apr 2014
Hi tanmoy,
The documentation for this function states something like the following (when you run help sparse from the command line):
sparse Create sparse matrix.
S = sparse(X) converts a sparse or full matrix to sparse form by squeezing
out any zero elements.
So you're right - the result of sparse on this matrix should be some kind of empty matrix since all elements are zero and so have been squeezed out. But before sparse is even called, zeros(80000) is invoked which will try to create a 80000x80000 matrix of zeros. So this is probably where you are getting stuck (and I noticed the same behaviour on my computer with your example). There is nothing wrong with the call to sparse , it is just that it follows the attempted creation of a huge matrix that chews up a lot of memory.
Hope that this helps!
Geoff
3 Commenti
Geoff Hayes
il 19 Apr 2014
What is telling you that MATLAB "eats up quite a bit of memory for this"? I just tried sparse(zeros(100)) and did not observe any problem…
Vedere anche
Categorie
Scopri di più su Sparse Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!