# Avoiding memory allocation for solves with multiple right-hand sides

**URL:** <https://discourse.julialang.org/t/avoiding-memory-allocation-for-solves-with-multiple-right-hand-sides/67879>\
**Category:** Performance\
**Tags:** linearalgebra, memory-allocation\
**Created:** [September 8, 2021, 3:36pm UTC](https://discourse.julialang.org/t/avoiding-memory-allocation-for-solves-with-multiple-right-hand-sides/67879 "2021-09-08T15:36:34Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2021, 4:59pm UTC](https://discourse.julialang.org/t/avoiding-memory-allocation-for-solves-with-multiple-right-hand-sides/67879/2 "2021-09-08T16:59:46Z")

</div>

It allocates because the operation `Kc\b` allocates, on the right-hand side. You either have to use an in-place version, or if the matrices are small, use static arrays:

```julia
julia> M = rand(3,3); b = rand(3);

julia> @btime x = $M \ $b;
  346.107 ns (3 allocations: 384 bytes)

julia> using StaticArrays

julia> M = @SMatrix(rand(3,3)); b = @SVector(rand(3));

julia> @btime x = $M \ $b;
  5.228 ns (0 allocations: 0 bytes)

```

Probably this is what you are looking for: [Non-allocating matrix inversion - #7 by stevengj](https://discourse.julialang.org/t/non-allocating-matrix-inversion/62264/7)

---

_[View the full topic](https://discourse.julialang.org/t/avoiding-memory-allocation-for-solves-with-multiple-right-hand-sides/67879)._
