# Understanding memory usage in Julia

**URL:** <https://discourse.julialang.org/t/understanding-memory-usage-in-julia/101647>\
**Category:** Performance\
**Tags:** memory, memory-allocation\
**Created:** [July 15, 2023, 8:20am UTC](https://discourse.julialang.org/t/understanding-memory-usage-in-julia/101647 "2023-07-15T08:20:00Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![pjuergens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjuergens/32/28380_2.png) [@pjuergens](https://discourse.julialang.org/u/pjuergens)\
**Post date:** [July 25, 2023, 2:50pm UTC](https://discourse.julialang.org/t/understanding-memory-usage-in-julia/101647/22 "2023-07-25T14:50:04Z")

</div>

that very much helped! Thanks a lot 🙂

---

<div class="post-metadata">

**Author:** ![Rkiefe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rkiefe/32/218845_2.png) [@Rkiefe](https://discourse.julialang.org/u/Rkiefe)\
**Post date:** [December 12, 2025, 2:54pm UTC](https://discourse.julialang.org/t/understanding-memory-usage-in-julia/101647/23 "2025-12-12T14:54:42Z")

</div>

I would like to add that as of now, Dec. 2025, I seem to be running to the same issue. I too am working on a FEM solver in Julia and during a loop where a large sparse stiffness matrix is updated, memory keeps on rising. Calling explicitly GC.gc() at the end of the loop fixed the issue, but this should not be needed. For reference, here is the code for the loop

```julia-auto
# Boundary conditions
RHS::Vector{Float64} = BoundaryIntegral(mesh, Hext, shell_id)

# Reset the permeability
mu .= mu0

# FEM
u::Vector{Float64} = zeros(mesh.nv+1)
  
Hfield::Matrix{Float64} = zeros(3, mesh.nt)
H::Vector{Float64} = zeros(mesh.nt)
Hold::Vector{Float64} = zeros(mesh.nt)

att::Int32 = 0
div::Float64 = Inf
while div > picardDeviation && att < maxAtt 

    att += 1
    Hold .= H

    # Updated the compressed stiffness matrix
    n = 0
    for i in 1:4
        for j in 1:4
            n += 1
            for k in 1:mesh.nt
                Acsc[(n-1)*mesh.nt + k] = Ak[n, k] * mu[k]
            end
        end
    end # Acsc

    # Update the global stiffness matrix
    A = sparse(rowIDs, colIDs, Acsc, mesh.nv, mesh.nv)

    # Magnetic scalar potential
    u = [A Lag;Lag' 0]\[-RHS;0]
    
    # Check solution
    if any(x -> !isfinite(x), u)
        error("Nans/Infs in the scalar potential")
    end

    # Magnetic field
    Hfield .= 0.0
    for k in 1:mesh.nt
        nds = mesh.t[:,k];

        # Sum the contributions
        for nd in nds
            # obtain the element parameters
            _, b, c, d = abcd(mesh.p,nds,nd)

            Hfield[1, k] -= u[nd]*b;
            Hfield[2, k] -= u[nd]*c;
            Hfield[3, k] -= u[nd]*d;
        end
    end

    # Magnetic field intensity
    for k in 1:mesh.nt
        H[k] = norm(Hfield[:, k])
    end

    # Update magnetic permeability            
    mu[mesh.InsideElements] .= spl(H[mesh.InsideElements])

    # Check deviation from previous result
    div = mu0*maximum(abs.(H[mesh.InsideElements].-Hold[mesh.InsideElements]))
    verbose ? println(att, " | mu0 |H(n)-H(n-1)| = ", div) : nothing

    # Ask for garbage collection because otherwise there is a memory build up
    # Still don't know why
    GC.gc()

end # Picard Iteration

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [December 12, 2025, 4:23pm UTC](https://discourse.julialang.org/t/understanding-memory-usage-in-julia/101647/24 "2025-12-12T16:23:25Z")

</div>

Which Julia version?

---

<div class="post-metadata">

**Author:** ![Rkiefe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rkiefe/32/218845_2.png) [@Rkiefe](https://discourse.julialang.org/u/Rkiefe)\
**Post date:** [December 12, 2025, 4:30pm UTC](https://discourse.julialang.org/t/understanding-memory-usage-in-julia/101647/25 "2025-12-12T16:30:39Z")

</div>

Version 1.12.2 , happens both on Windows and Linux (Ubuntu 24.04)  
I also created a comment on this github issue, and seems to be getting some traction. [https://github.com/JuliaLang/julia/issues/50658#issuecomment-3647135647](https://github.com/JuliaLang/julia/issues/50658#issuecomment-3647135647)

[Previous page](https://discourse.julialang.org/t/understanding-memory-usage-in-julia/101647.md?page=1)
