# Running out of RAM with repeated cholesky solves

**URL:** <https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044>\
**Category:** Numerics\
**Tags:** linearalgebra, cholesky\
**Created:** [March 5, 2026, 5:11pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044 "2026-03-05T17:11:12Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![falconflier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/falconflier/32/217857_2.png) [@falconflier](https://discourse.julialang.org/u/falconflier)\
**Post date:** [March 5, 2026, 5:11pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/1 "2026-03-05T17:11:12Z")

</div>

Hello,

I have a system in which I need to repeatedly solve an electrostatics problem with an evolving charge configuration. When I left my program running overnight, I came back to find that RAM utilization had accumulated to around 30GB and the program had slowed to a crawl. I believe I’ve located the problem line  
`ldiv!(solution, stiff_factorization, force_vector)`  
When I comment this out, the program is able to complete with well under ~1GB of RAM allocation. I’m wondering if I need to manually clean up any memory being used by the solver. I tried to create a small example of what I’m working with, but memory doesn’t seem to accumulate in the same way. I’m not very familiar with the workings of the Garbage Collector, so any advice about how to avoid a memory leak over longer runs is appreciated. Thank you!

```julia-auto
using JLD2
using LinearAlgebra

stiffness_matrix = load("./dev/ldiv_memory/stiff_mat.jld2", "stiff_mat")
issymmetric(stiffness_matrix)
factorization = cholesky(stiffness_matrix)

dim = size(stiffness_matrix, 1)
sol = zeros(dim)

for i=1:1_000_000
    force_vector = rand(dim)
    ldiv!(sol, factorization, force_vector)
end

```

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [March 5, 2026, 5:24pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/2 "2026-03-05T17:24:24Z")

</div>

What Julia version are you using. I fixed something similar and it landed on 1.12.5 so if you could try there

---

<div class="post-metadata">

**Author:** ![falconflier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/falconflier/32/217857_2.png) [@falconflier](https://discourse.julialang.org/u/falconflier)\
**Post date:** [March 5, 2026, 5:47pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/3 "2026-03-05T17:47:02Z")

</div>

I just ran `juliaup update` and `up` in the package manager. I’m on the 1.12.5 release, but task manager (Windows) keeps showing the memory increasing. Previously, I tried using cg! which seemed to reduce the memory load but it also seems to be running more slowly for my matrix.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 5, 2026, 6:52pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/4 "2026-03-05T18:52:46Z")

</div>

You really shouldn’t need to manually clean up memory. In the Windows task manager, what memory metric are you looking at? (Depending on how you measure memory it can be deceptive due to virtual memory.)

Is `stiffness_matrix` a dense matrix or a sparse one?

(In the latter case, it might be a memory leak in the external CHOLMOD library that we are calling for sparse Cholesky solves? Though this would be surprising… that library has been around for a long while.)

> [@falconflier](#):
>
> the program had slowed to a crawl.

Are you sure it’s not something else? For example, if NaN’s have crept into your vectors then things will slow to a crawl due to floating-point exceptions.

> [@falconflier](#):
>
> I tried to create a small example of what I’m working with, but memory doesn’t seem to accumulate in the same way.

Does the example code you posted exhibit the problem (if you use it with your `stiff_mat.jld2`) or not?

---

<div class="post-metadata">

**Author:** ![falconflier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/falconflier/32/217857_2.png) [@falconflier](https://discourse.julialang.org/u/falconflier)\
**Post date:** [March 6, 2026, 5:38am UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/5 "2026-03-06T05:38:12Z")

</div>

Thanks for the feedback, it led me down some interesting investigations. My stiffness matrix is assembled as a dimension ~5000 sparse matrix `SparseMatrixCSC{Float64, Int64}` but I had cast it to a dense matrix when I saved it. I’ve modified the snippet to cast to a sparse matrix, and now I see a steady memory usage increase that drops down after I terminated the program after 665800 iterations.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d8a8338d99ce84e6b87ec60fbb12f0d60c295e8.png)  
My full program drastically slows down after the memory allocation reaches 80%, which is why I suspect that it’s not just the way that task manager measures memory usage. I’m using the solution vector for downstream calculations, which haven’t thrown an errors for values being too large (I can try to re-run with an explicit NaN check as well).

I tried to create a version of the code that doesn’t need the file to run (Also happy to provide the saved matrix but it seems I’m not allowed to upload it here). The synthetic random matrix takes a lot longer to churn through iterations but I think I see memory uptick as well

```julia-auto
using JLD2
using LinearAlgebra
using SparseArrays

load_file = false
if load_file
    stiffness_matrix = load("./stiff_mat.jld2", "stiff_mat")
    println(typeof(stiffness_matrix))
    stiffness_matrix = SparseMatrixCSC(stiffness_matrix)
    println(typeof(stiffness_matrix))
else
    mock_size = 5000
    mock_density = 0.002
    A = sprand(Float64, mock_size, mock_size, mock_density)
    S = A + A'
    
    # 3. Make it strictly diagonally dominant to guarantee Positive Definiteness
    # Since S has positive entries, sum() gives the sum of absolute off-diagonals
    row_sums = vec(sum(S, dims=2))
    
    # Create a diagonal matrix where each diagonal is slightly larger than the row sum
    D = spdiagm(0 => row_sums .+ 1.0)
    
    stiffness_matrix = SparseMatrixCSC{Float64, Int64}(S + D)
end
println("Is matrix symmetric: $(issymmetric(stiffness_matrix)), determinant: $(det(stiffness_matrix))")
factorization = cholesky(stiffness_matrix)

dim = size(stiffness_matrix, 1)
sol = zeros(dim)

for i=1:1_000_000
    force_vector = rand(dim)
    if i%100 == 0
        println("Iteration $(i)")
    end
    ldiv!(sol, factorization, force_vector)
    if !all(isfinite.(sol))
        throw(ErrorException("Got a non-finite solution"))
    end
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 6, 2026, 12:29pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/6 "2026-03-06T12:29:43Z")

</div>

> [@falconflier](#):
>
> (Also happy to provide the saved matrix but it seems I’m not allowed to upload it here)

You can upload it elsewhere (e.g. google drive, github gist, dropbox, …) and post a link here.

---

<div class="post-metadata">

**Author:** ![falconflier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/falconflier/32/217857_2.png) [@falconflier](https://discourse.julialang.org/u/falconflier)\
**Post date:** [March 6, 2026, 3:53pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/7 "2026-03-06T15:53:28Z")

</div>

Uploaded to Google drive: [https://drive.google.com/file/d/1GyU1ZflSY9-WyAMQKhVQu0me2mUIK\_IO/view?usp=drive\_link](https://drive.google.com/file/d/1GyU1ZflSY9-WyAMQKhVQu0me2mUIK_IO/view?usp=drive_link)

---

<div class="post-metadata">

**Author:** ![tduretz](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tduretz](https://discourse.julialang.org/u/tduretz)\
**Post date:** [July 6, 2026, 5:34am UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/8 "2026-07-06T05:34:21Z")

</div>

Hi, was there any update regarding this issue? We observe a similar behaviour with another code, eventually leading to a crash of the session.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [July 6, 2026, 11:09am UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/9 "2026-07-06T11:09:33Z")

</div>

I think this is a memory leak [here](https://github.com/JuliaSparse/SparseArrays.jl/blame/896723afccc0833aee2b2131ec0d444857caa8c2/src/solvers/cholmod.jl#L1966).  
The relevant code snippet:

```julia-auto
        Y_Handle = Ptr{cholmod_dense_struct}(C_NULL)
[...]
        status = GC.@preserve x dense_x b dense_b begin
            $(cholname(:solve2, TI))(
                CHOLMOD_A, L,
                Ref(dense_b), C_NULL,
                Ref(X_Handle), C_NULL,
                Ref(Y_Handle),
                Ref(E_Handle),
                getcommon($TI))
        end
        if Y_Handle != C_NULL
            free!(Y_Handle)
        end

```

It works that way in C, but not in julia. It must be `Y_Handle = Ref(C_NULL)`, and then `if Y_Handle[] != C_NULL ....`.

PS [CHOLMOD allocations · Issue #726 · JuliaSparse/SparseArrays.jl · GitHub](https://github.com/JuliaSparse/SparseArrays.jl/issues/726)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 6, 2026, 11:35am UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/10 "2026-07-06T11:35:24Z")

</div>

> [@foobar\_lv2](#):
>
> I think this is a memory leak [here](https://github.com/JuliaSparse/SparseArrays.jl/blame/896723afccc0833aee2b2131ec0d444857caa8c2/src/solvers/cholmod.jl#L1966).

Thanks: [fix memory leak in cholmod - Pull Request #733 - JuliaSparse/SparseArrays.jl - GitHub](https://github.com/JuliaSparse/SparseArrays.jl/pull/733)

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [July 8, 2026, 3:06pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/11 "2026-07-08T15:06:43Z")

</div>

Thanks for identifying this issue and fixing it so quickly! I would like to install this patch in my Julia environment now rather than wait until it becomes part of a future release. Are there instructions posted somewhere on how to do this (i.e., how to modify the SparseArrays standard library in an existing installation)? Thanks.

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [July 8, 2026, 3:37pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/12 "2026-07-08T15:37:03Z")

</div>

Will this work?

```julia-auto
pkg> rm SparseArrays
pkg> add https://github.com/stevengj/SparseArrays.jl#patch-4

```

This should install @stevengj’s patched version into your environment.

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [July 8, 2026, 4:36pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/13 "2026-07-08T16:36:15Z")

</div>

Thanks! This did not work with Julia 1.12.6, but it worked after updating to 1.13-rc1. Now I will see if it makes a difference in my application.

---

<div class="post-metadata">

**Author:** ![tduretz](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tduretz](https://discourse.julialang.org/u/tduretz)\
**Post date:** [September 1, 2026, 5:26pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/14 "2026-09-01T17:26:01Z")

</div>

Is this patch included in 1.13. I don’t seem find a mention in the [release notes](https://github.com/JuliaLang/julia/blob/v1.13.0-rc3/NEWS.md). @TimG did you get a chance to try the patch?

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [September 1, 2026, 7:56pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/15 "2026-09-01T19:56:21Z")

</div>

I don’t use sparse arrays, I’m afraid. I’m unlikely to try it. Someone else, maybe…

---

<div class="post-metadata">

**Author:** ![araujoms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/araujoms/32/217734_2.png) [@araujoms](https://discourse.julialang.org/u/araujoms)\
**Post date:** [September 1, 2026, 10:53pm UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/16 "2026-09-01T22:53:45Z")

</div>

This patch is included in 1.13, but hasn’t been released yet. You will see it in the next 1.13 version, either the final release or another release candidate.

---

<div class="post-metadata">

**Author:** ![araujoms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/araujoms/32/217734_2.png) [@araujoms](https://discourse.julialang.org/u/araujoms)\
**Post date:** [September 3, 2026, 8:58am UTC](https://discourse.julialang.org/t/running-out-of-ram-with-repeated-cholesky-solves/136044/17 "2026-09-03T08:58:43Z")

</div>

And indeed it has now been released with 1.13-rc4.
