# Memory allocation for sparse matrix multiplication

**URL:** <https://discourse.julialang.org/t/memory-allocation-for-sparse-matrix-multiplication/37787>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 17, 2020, 10:20pm UTC](https://discourse.julialang.org/t/memory-allocation-for-sparse-matrix-multiplication/37787 "2020-04-17T22:20:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![volkerkarle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/volkerkarle/32/14143_2.png) [@volkerkarle](https://discourse.julialang.org/u/volkerkarle)\
**Post date:** [April 17, 2020, 10:20pm UTC](https://discourse.julialang.org/t/memory-allocation-for-sparse-matrix-multiplication/37787/1 "2020-04-17T22:20:50Z")

</div>

Dear folks,

This is my first post, so feel free to relocate me to an appropriate tag/ group/ domain, etc. etc.  
So I have the following questions: 1) How can I estimate the amount of memory needed for the multiplication of two sparse matrices, given their sparsity and size 2) how can I do that more memory-efficient for the calculation of a projector? Have a look at the following code:

```julia
using LinearAlgebra
using SparseArrays
N = 200000
d = 1e-4
A = sprand(N,N,d)
@time P = A*A'

```

This works out, but it takes much more memory than I anticipated.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bcmichael](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcmichael/32/10003_2.png) [@bcmichael](https://discourse.julialang.org/u/bcmichael)\
**Post date:** [April 17, 2020, 11:00pm UTC](https://discourse.julialang.org/t/memory-allocation-for-sparse-matrix-multiplication/37787/2 "2020-04-17T23:00:33Z")

</div>

The probability of any element in P being nonzero is the probability that there is any index into the corresponding row and column in A and A’ where both values are nonzero (with the exception of the diagonal entries of P, which are all nonzero). That’s approximately `N*d*d`, which for your values is about 0.002. Given how sparse matrices are stored that means P should need about `N*N*0.002*2*8` bytes of memory, plus a bit more for the column pointers. That’s ballpark 1.3 GB.
