Increase the ‘aggressiveness’ of the garbage collection?

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

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().

1 Like

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

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?

3 Likes

To expand on this: measuring RAM usage is pretty complicated (Memory Measurements Complexities and Considerations - 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.

1 Like

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

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

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

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

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)

2 Likes

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”) ?

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.