# Ever increasing time for garbage collection

**URL:** <https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [May 23, 2022, 9:48pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543 "2022-05-23T21:48:35Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [May 23, 2022, 9:48pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/1 "2022-05-23T21:48:35Z")

</div>

Hello,  
I have a simulation that is running in real-time for an undefined period of time (until you stop it).  
I solve my equation system every 50ms. For one solution about 800 calls to the inner residual function are needed. This works fine.

The problem I have is that the time needed for the garbage collection is continuously increasing.  
 ![joystick](https://global.discourse-cdn.com/julialang/original/3X/5/c/5cfafbd6d311b64766c3467be48345c3ab2ea898.png)

I do an incremental garbage collection once per time step like this:

```julia
t_gc_tot = @elapsed GC.gc(false)

```

After 300s of simulation time already 12ms time are needed for the garbage collection, and if you extrapolate after 1200s the simulation will fail because most of the time is used for gc.

What can I do to solve this issue?

I have zero allocations in my simulation code, but the solver (IDA solver from Sundials with GMRES linear solver) is allocating a lot.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 23, 2022, 9:53pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/2 "2022-05-23T21:53:57Z")

</div>

That’s really not supposed to happen. If you get a mwe that reproduces the behavior, it would be good to have it in [GitHub - JuliaCI/GCBenchmarks](https://github.com/JuliaCI/GCBenchmarks)

---

<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:** [May 23, 2022, 10:04pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/3 "2022-05-23T22:04:52Z")

</div>

If you check out this repository: [GitHub - aenarete/KiteViewers.jl: 3D viewer for airborne wind energy systems](https://github.com/aenarete/KiteViewers.jl)  
And then execute:

```julia
include("examples/depower_simple.jl")

```

you get a plot where you can already see the increasing gc time…

But without active control you cannot run the simulation longer than 50s. If you have a joystick you can control the kite and run:

```julia
include("examples/joystick.jl")

```

which I did to produce the example in the last post.

But for a nice MWE I would need an autopilot which might be ready in a week…

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [May 23, 2022, 10:08pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/4 "2022-05-23T22:08:02Z")

</div>

It would be important to understand if the amount of memory stays constant or also increases evermore. The GC should only increase as function of heap\_size and object-graph size. So this sounds like something is allocating more and more, but never releasing any of that memory,

---

<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:** [May 23, 2022, 10:14pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/5 "2022-05-23T22:14:22Z")

</div>

I can try to disable the Makie based GUI and see if the problem persists…

How can I measure the memory that is used from within Julia?

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 23, 2022, 10:22pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/6 "2022-05-23T22:22:27Z")

</div>

Did you maybe hit this:

[https://github.com/JuliaLang/julia/issues/45068](https://github.com/JuliaLang/julia/issues/45068)

---

<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:** [May 23, 2022, 10:32pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/7 "2022-05-23T22:32:11Z")

</div>

No, I did not turn GC off in my test…

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 23, 2022, 11:10pm UTC](https://discourse.julialang.org/t/ever-increasing-time-for-garbage-collection/81543/8 "2022-05-23T23:10:55Z")

</div>

> [@ufechner7](#):
>
> I have zero allocations in my simulation code, but the solver (IDA solver from Sundials with GMRES linear solver) is allocating a lot.

Then I would look into is it also deallocating a lot (and look at max. mem. used, somehow).

I.e. I would look into if you’re using it wrong, or even if it has a bug. I suppose the underlying C code is ok.

I did look up e.g. Alloc, free, malloc (only found some constants that way), in the wrapper, and found some matches for all.

Do you need to do something with handles?

> <https://github.com/SciML/Sundials.jl/blob/b88762bdaf4141d1c4363cff8f6175cfca937f39/src/handle.jl#L48>

I’m not sure what this default early\_free = false is, but likely a false alarm:

> <https://github.com/SciML/Sundials.jl/blob/b88762bdaf4141d1c4363cff8f6175cfca937f39/src/common_interface/solve.jl#L1467>
