Memory usage in sparse matrix
39 次查看(过去 30 天)
显示 更早的评论
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 个评论
Matt J
2014-4-19
编辑:Matt J
2014-4-19
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
2014-4-21
编辑:James Tursa
2014-4-21
@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.
采纳的回答
Ken Atwell
2014-4-19
Try just:
>> sparse(80000,80000)
to avoid the temporary allocation of a 80000^2 non-sparse zeros array.
2 个评论
Ken Atwell
2014-4-20
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.
更多回答(1 个)
Geoff Hayes
2014-4-19
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 个评论
Geoff Hayes
2014-4-19
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…
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!