# Strange Memory Allocations with DifferentialEquations

**URL:** https://discourse.julialang.org/t/strange-memory-allocations-with-differentialequations/83909
**Category:** Numerics
**Tags:** question, memory, memory-allocation, differentialequation
**Created:** [July 7, 2022, 3:58pm UTC](https://discourse.julialang.org/t/strange-memory-allocations-with-differentialequations/83909 "2022-07-07T15:58:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![7vn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/7vn/32/50749_2.png) [@7vn](https://discourse.julialang.org/u/7vn)
#### Post date: [July 7, 2022, 3:58pm UTC](https://discourse.julialang.org/t/strange-memory-allocations-with-differentialequations/83909/1 "2022-07-07T15:58:43Z")

</div>

Hello,

I’m observing very strange and large memory allocations while solving a standard ODE and evaluating it at given times. However, after the first benchmark, if (and only if!) I recompile the integrated function these allocations disappear.

```julia
using BenchmarkTools
import DifferentialEquations: Vern9, ODEProblem, solve

```

The function I’m integrating is:

```julia

@inbounds function dyn_twobody!(du::Vector{Float64}, u::Vector{Float64}, 
                                 GM::Float64, t::Float64)

    r = (u[1]*u[1] + u[2]*u[2] + u[3]*u[3])^1.5

    du[1] = u[4]
    du[2] = u[5]
    du[3] = u[6]
    du[4] = -GM*u[1]/r
    du[5] = -GM*u[2]/r
    du[6] = -GM*u[3]/r
    
    nothing 
end

```

while `test` is the function that evaluates the solution at the given times:

```julia

function test(x::Vector{Float64}, tms::Vector{Float64})

    GM_MOON = 4.902780137400001e3;
    user_prob = ODEProblem(dyn_twobody!, x, [0., 86400], GM_MOON)

    user_sol = solve(user_prob, Vern9(), abstol=1e-10, reltol=1e-9, saveat=tms, save_everystep=false).u

    return user_sol 
end

```

Calling it with:

```julia
x = [50500., 232., 32321., -1.2, 0.01, 0.3];
tms = collect(LinRange(0., 86400., 86400));

@benchmark test($x, $tms)

```

yields:

```julia
BenchmarkTools.Trial: 30 samples with 1 evaluation.
 Range (min … max): 129.553 ms … 535.830 ms ┊ GC (min … max): 11.32% … 74.42%
 Time (median): 151.338 ms ┊ GC (median): 14.82%
 Time (mean ± σ): 167.691 ms ± 71.469 ms ┊ GC (mean ± σ): 21.46% ± 11.46%

    ██                                                           
  ▆▇██▆▁▆▁▆▄▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄ ▁
  130 ms Histogram: frequency by time 536 ms <

 Memory estimate: 304.61 MiB, allocs estimate: 432505.

```

If i recompile `dyn_twobody!` the new benchmark is:

```julia
BenchmarkTools.Trial: 81 samples with 1 evaluation.
 Range (min … max): 49.294 ms … 106.972 ms ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 59.441 ms ┊ GC (median): 0.00%
 Time (mean ± σ): 62.334 ms ± 12.016 ms ┊ GC (mean ± σ): 2.41% ± 4.19%

  █ ▁ ▁▁ ▃                                                
  █▆█▇▄▇▇██▇▆▇▇█▁▇▆▆▆▆▄▆▆▆▄▄▁▁▄▁▁▆▆▁▁▁▄▁▁▁▁▁▁▁▆▁▁▄▁▄▄▁▁▁▁▁▁▁▁▄ ▁
  49.3 ms Histogram: frequency by time 98.8 ms <

 Memory estimate: 17.22 MiB, allocs estimate: 173311.

```

Any idea what’s happening with my code? I see the Garbage Collector is taking quite a lot of time

---

<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: [July 20, 2022, 12:24pm UTC](https://discourse.julialang.org/t/strange-memory-allocations-with-differentialequations/83909/2 "2022-07-20T12:24:22Z")

</div>

Seems like something in the inlinling heuristics? I’ll try and get a compiler person to take a look. Can you open an issue?

---

<div class="post-metadata">

### Author: ![7vn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/7vn/32/50749_2.png) [@7vn](https://discourse.julialang.org/u/7vn)
#### Post date: [July 25, 2022, 10:03pm UTC](https://discourse.julialang.org/t/strange-memory-allocations-with-differentialequations/83909/3 "2022-07-25T22:03:35Z")

</div>

Hello and thanks for answering, actually I had already opened the very same issue here: [Strange Memory Allocations with saveat · Issue #880 · SciML/DifferentialEquations.jl · GitHub](https://github.com/SciML/DifferentialEquations.jl/issues/880)

I plan to further investigate it as soon as I have more time. In the meantime, couldn’t it be associated to some wrong type inference in the dense functions of DE.jl? Because, irrespective of the function I’m integrating, I always get the same behavior

---

<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: [July 26, 2022, 2:33am UTC](https://discourse.julialang.org/t/strange-memory-allocations-with-differentialequations/83909/4 "2022-07-26T02:33:58Z")

</div>

> [@7vn](#):
>
> I plan to further investigate it as soon as I have more time. In the meantime, couldn’t it be associated to some wrong type inference in the dense functions of DE.jl? Because, irrespective of the function I’m integrating, I always get the same behavior

Maybe type inference, or maybe it’s just inlining heuristics. @aviatesk mentioned that it could just be because when running the function building with a different cache you could get different inference and inlining behavior. So maybe one spot just needs `@inline` and it goes away? It’s hard to tell.
