# Increase the ‘aggressiveness’ of the garbage collection?

**URL:** <https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750>\
**Category:** General Usage\
**Created:** [December 16, 2022, 4:07pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750 "2022-12-16T16:07:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [December 16, 2022, 4:07pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/1 "2022-12-16T16:07:25Z")

</div>

I am working with large arrays, and repeatedly resort to calling GC.gc() manually in cases  
like this:

```julia
A = load_large_array();
do_stuff_with_large_array!(A)
save_results(A)
A = nothing
GC.gc()
A = load_another_large_array();

```

Otherwise, calling `load_another_large_array()` doubles the memory usage. Is there some way to increase the ‘aggressiveness’ of the garbage collection?

I also noticed that if I call `resize!(A, small_size)` the memory usage does not decrease even after `GC.gc()`.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 16, 2022, 5:19pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/2 "2022-12-16T17:19:31Z")

</div>

> [@Mikhail\_Kagalenko](#):
>
> if I call `resize!(A, small_size)` the memory usage does not decrease even after `GC.gc()`.

This feature makes growing the array again fast and allocation-free.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 16, 2022, 5:40pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/3 "2022-12-16T17:40:54Z")

</div>

A few quick notes here — you may already know these things:

- Julia’s GC doesn’t “run in the background” — simply setting `A = nothing` won’t trigger a GC run. What will happen is that the _next time_ you allocate, GC will check to see what the “memory pressure” on the system is like and if it needs to look for abandoned objects.
- Julia v1.9 should work significantly better in terms of identifying memory pressure in containerized systems
- The goal isn’t to maintain a small memory footprint for the sake of a small footprint — it just wants to fit in the system.

Are you having trouble keeping Julia from OOM’ing the system?

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [December 16, 2022, 8:25pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/4 "2022-12-16T20:25:13Z")

</div>

To expand on this: measuring RAM usage is pretty complicated ([Memory Measurements Complexities and Considerations - Part 1](https://community.ibm.com/community/user/aiops/blogs/riley-zimmerman/2021/07/01/memory-measurements-part-1) looks like a good starting point if you want to understand more). On top of that, julia has no particular reason to give back RAM it has used if the OS doesn’t need it, so `top` will often give high numbers without it ever being an issue.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [December 19, 2022, 5:01pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/5 "2022-12-19T17:01:21Z")

</div>

Perhaps, that behaviour should be tunable via an optional parameter?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [December 19, 2022, 5:04pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/6 "2022-12-19T17:04:34Z")

</div>

> [@mbauman](#):
>
> the _next time_ you allocate, GC will check to see what the “memory pressure” on the system is like and if it needs to look for abandoned objects.

That sounds like what I am thinking about - I want to increase the “memory pressure”

> [@mbauman](#):
>
> Are you having trouble keeping Julia from OOM’ing the system?

No, I would like to prevent it from swapping heavily on a weaker computer with other programs runnning.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [December 19, 2022, 5:06pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/7 "2022-12-19T17:06:14Z")

</div>

> [@antoine-levitt](#):
>
> On top of that, julia has no particular reason to give back RAM it has used if the OS doesn’t need it

Can I tell it to give back spare RAM if its size gets big?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 19, 2022, 5:10pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/8 "2022-12-19T17:10:59Z")

</div>

There is a new `--heap-size-hint` flag in (the upcoming) Julia 1.9 that might be useful ([added new command line option heap\_size\_hint for greedy GC by rssdev10 · Pull Request #45369 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/45369))

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [December 19, 2022, 5:19pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/9 "2022-12-19T17:19:39Z")

</div>

That looks like what I am looking for. Is there a reason it can not be tuned at runtime by something like `GC.heap\_size\_hint(“1GB”) ?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 19, 2022, 6:39pm UTC](https://discourse.julialang.org/t/increase-the-aggressiveness-of-the-garbage-collection/91750/10 "2022-12-19T18:39:48Z")

</div>

It just ends up doing a call to `jl_gc_set_max_memory` (which you could also just `ccall` from Julia) so it seems to me that it should indeed be callable during runtime.
