稀疏矩阵的内存使用问题


#1

稀疏矩阵在计算中多出来的内存分配是什么原因呢?

using LinearAlgebra
using BenchmarkTools

a = Matrix{Float64}(I,19000,19000)
b = collect(1:19000)
@benchmark a\b

BenchmarkTools.Trial: 
  memory estimate:  297.05 KiB
  allocs estimate:  5
  --------------
  minimum time:     567.951 ms (0.00% GC)
  median time:      580.827 ms (0.00% GC)
  mean time:        581.117 ms (0.00% GC)
  maximum time:     591.778 ms (0.00% GC)
  --------------
  samples:          9
  evals/sample:     1

改成稀疏矩阵之后:

using LinearAlgebra
using SparseArrays
using BenchmarkTools

a = Matrix{Float64}(I,19000,19000)
A = sparse(a)
b = collect(1:19000)
@benchmark A\b

BenchmarkTools.Trial: 
  memory estimate:  1.29 MiB
  allocs estimate:  36
  --------------
  minimum time:     599.900 μs (0.00% GC)
  median time:      611.400 μs (0.00% GC)
  mean time:        689.528 μs (4.81% GC)
  maximum time:     4.653 ms (76.26% GC)
  --------------
  samples:          7179
  evals/sample:     1

我觉得改成稀疏矩阵之后不是应该内存分配更少吗?还是我的理解有问题:joy: