# Reducing RAM usage when solving large set of differential equations

**URL:** <https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 6, 2025, 9:55am UTC](https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610 "2025-02-06T09:55:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![diffeqslvr](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@diffeqslvr](https://discourse.julialang.org/u/diffeqslvr)\
**Post date:** [February 6, 2025, 9:55am UTC](https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610/1 "2025-02-06T09:55:26Z")

</div>

I am solving a large system of differential equations (O(500 000) equations). Unsurprisingly, I run into RAM issues. I already tried to only save a subset of O(13 000) variables using a SavingCallback

```julia
saved_values = SavedValues(Float64, Vector{ComplexF64})
function extract(u, t, integrator)
      return u[save_subset]
end
cb = SavingCallback(extract, saved_values, saveat = 0.1)
tspan = (0.0, 70.0)
prob = ODEProblem(eoms!, σ₀, tspan, p)
sol = solve(
     prob,
     Tsit5(),
     callback = cb,
     dtmax = 0.1,
     maxiters = 100000,
     saveat = 70.0, # only save full solution at the end
)

```

My last job failed with a MaxRSS of 180 GB. Is this something one would expect? Are there more any ways to reduce the RAM usage further?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [February 6, 2025, 12:51pm UTC](https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610/2 "2025-02-06T12:51:54Z")

</div>

There are some things you can try:

- make sure you use the inplace, i.e. computing `du!` does not allocate. If you need scratch space to compute `du` then you can preallocate it and put it in the parameters.
- try starting Julia with `--heap-size-hint 120G`. Experiment a bit the value you put there. It should be smaller than the max available to give Julia the opportunity to run a GC before going OOM.
- you don’t need to set `dt_max` to force saving at specific locations. Try leaving it unspecified

---

<div class="post-metadata">

**Author:** ![diffeqslvr](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@diffeqslvr](https://discourse.julialang.org/u/diffeqslvr)\
**Post date:** [February 6, 2025, 2:42pm UTC](https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610/3 "2025-02-06T14:42:33Z")

</div>

I will try using —heap-size-hint and not setting dtmax, thanks!

You were right, I do use some allocation when calculating du, but I struggle not to. The variables that I allocate are much smaller than 180GB, and they should be dealt with by the garbage collector, right?

Two more pieces to the puzzle:

- I use multiple threads when calculating du. Might this have an impact?
- The computation only fails after several hours. The RAM must thus be growing due to some reason.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 6, 2025, 2:53pm UTC](https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610/4 "2025-02-06T14:53:52Z")

</div>

Standard ODE solvers can have many cache variables in order to achieve the performance. There’s specialized low-storage RK methods for this kind of situation:

> **[ODE Solvers · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/solvers/ode_solve/#Low-Storage-Methods)**
>
> Documentation for DifferentialEquations.jl.

---

<div class="post-metadata">

**Author:** ![diffeqslvr](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@diffeqslvr](https://discourse.julialang.org/u/diffeqslvr)\
**Post date:** [February 6, 2025, 3:25pm UTC](https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610/5 "2025-02-06T15:25:31Z")

</div>

Which one would you recommend for instead of Tsit5? Thanks!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 6, 2025, 3:27pm UTC](https://discourse.julialang.org/t/reducing-ram-usage-when-solving-large-set-of-differential-equations/125610/6 "2025-02-06T15:27:57Z")

</div>

Hard to tell without knowing your PDE. But maybe `RDPK3Sp35`.
