# Julia GC, heap fragmentation, out of memory, push!/append!

**URL:** <https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224>\
**Category:** Internals & Design\
**Created:** [August 3, 2022, 11:52am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224 "2022-08-03T11:52:17Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)\
**Post date:** [August 10, 2022, 8:43am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/21 "2022-08-10T08:43:16Z")

</div>

Nice. I didn’t know that. Guess I’m too old school 😉  
Although I deploy on Linux I develop on Windows and there you can still get into the situation where you get to 100% RAM taken and then the julia GC frantically trying to free memory and very large (GBs) fluctuations in julia memory but never stabilizing as long as you keep working (doing requests).

---

<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:** [August 10, 2022, 10:30am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/22 "2022-08-10T10:30:42Z")

</div>

Thanks for clarifying, not sure how often in practice the address space gets (very) fragmented in practice. I may have downplayed the risk too much. I guess there’s a reason Ruby looked into compacting GC (e.g. for long-running web servers), may or may not?) apply less to more typical HPC use of Julia. Anyway there’s a solution with MESH I posted in my latest post. I think you can do that right now with Julia without code changes in Julia, maybe there’s already some benefit (or none). It may need the same simplification of Julia code I mentioned for mimalloc, to see all the free that would happen.

> [@dlakelan](#):
>
> so you have 100MB allocated, but in 10GB of virtual address

This assumes the OS doesn’t do anything clever with the holes in virtual address space, and I suppose it can’t because they were allocated as memory (maybe used previously), and the OS never informed not free/garbage by now. I suppose it could, and would need help from the libc. Or just allocate very large swap file, it’s cheaper than RAM. Or use MESH.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 10, 2022, 1:46pm UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/23 "2022-08-10T13:46:03Z")

</div>

Incidentally, it’s very likely this issue [bit me recently…](https://discourse.julialang.org/t/determining-size-of-dataframe-for-memory-management/85277/36)

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [November 22, 2022, 9:07am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/24 "2022-11-22T09:07:21Z")

</div>

Many poople is being hurted by this problem, I am facing the same problem.

> [@The relationship between exsting julia objects and gc time?](https://discourse.julialang.org/t/the-relationship-between-exsting-julia-objects-and-gc-time/90533):
>
> I found that, with more and more large objects generated in running time, the gc time will increase massively. So I have to always pay much attention on generating objects. Is there a way I can handle this properly?

I have a computer with RAM 128G, but because of this problem, it turns out that the big RAM is useless.

---

<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 16, 2024, 10:52am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/25 "2024-05-16T10:52:18Z")

</div>

> [@Palli](#):
>
> It’s time to revisit mimalloc (I looked into linking alternatives to Julia before):

I really think so, even more now since FYI: Python 3.13 beta1 is out and it has:

> - A modified version of [mimalloc](https://github.com/microsoft/mimalloc) is now included, optional but enabled by default if supported by the platform, and required for the free-threaded build mode.

I didn’t check which:

> Latest release tag: `v2.1.6` (2024-05-13).  
> Latest v1 tag: `v1.8.6` (2024-05-13).

Mimalloc was the best malloc some time back when I checked, and I doubt it has changed. Do we need the standard one, or same as Python’s modified? As with OpenSSL I’m thinking might there be a conflict, I’m not sure you can different mallocs in the same program, or in general different .so versions, except by workarounds (as discussed at Julia’s OpenSSL issue).

Off-topic, but Python 3.13 is going to be a very intriguing, and important released with:

> - An [_experimental_ free-threaded build mode](https://docs.python.org/3.13/whatsnew/3.13.html#free-threaded-cpython), which disables the Global Interpreter Lock, allowing threads to run more concurrently.
> - A [preliminary, _experimental_ JIT](https://docs.python.org/3.13/whatsnew/3.13.html#experimental-jit-compiler), providing the ground work for significant performance improvements.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 16, 2024, 6:11pm UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/26 "2024-05-16T18:11:02Z")

</div>

I’m not sure it was worth reviving the thread.

An arena allocation scheme partly addresses the original issue.

> **[GitHub - MasonProtter/Bumper.jl: Bring Your Own Stack](https://github.com/MasonProtter/Bumper.jl)**
>
> Bring Your Own Stack. Contribute to MasonProtter/Bumper.jl development by creating an account on GitHub.

As for distinct allocators, I’m not sure if that is a particular issue as long as you don’t try to free something you did not allocate.

For example, I played with integrating alternative allocators in ArrayAllocators.jl:

> **[GitHub - mkitti/ArrayAllocators.jl: Allocate arrays with malloc, calloc, or...](https://github.com/mkitti/ArrayAllocators.jl)**
>
> Allocate arrays with malloc, calloc, or on NUMA nodes - mkitti/ArrayAllocators.jl

The basic idea was to use `unsafe_wrap` with `own = false` to tell Julia _not_ to free that memory by itself.

The one problem with this is I was not able to tell the Julia GC how much memory I had allocated outside of it, while still having to rely on finalization to cleanup.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2024, 5:51am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/27 "2024-05-20T05:51:46Z")

</div>

> [@mkitti](#):
>
> An arena allocation scheme partly addresses the original issue.

Perhaps, but I tried running a Genie webserver for running a survey over a few weeks, and the memory usage kept on growing and growing. After a few server OOMs, I set it to restart (and reload the data it previously had in memory), and that worked but lead a ~5 minutes of outage every day (which people hit, since I had thousands of respondents). I suspect this is an instance of the same issue, and harder to solve by “just using an allocation arena for your array”.

As such, I’m rather interested in the prospect of trying something like Mesh in Julia, but I see that Windows support is still a WIP there.

---

<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 20, 2024, 7:41am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/28 "2024-05-20T07:41:27Z")

</div>

> [@tecosaur](#):
>
> memory usage kept on growing and growing

Was this with Julia 1.10?

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2024, 7:47am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/29 "2024-05-20T07:47:15Z")

</div>

> [@ufechner7](#):
>
> Was this with Julia 1.10?

1.8, this was a while ago.

---

<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 20, 2024, 7:48am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/30 "2024-05-20T07:48:34Z")

</div>

> [@tecosaur](#):
>
> 1.8, this was a while ago.

The GC improved at lot in 1.10, I have no more OOM events since this version…

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2024, 7:53am UTC](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224/31 "2024-05-20T07:53:44Z")

</div>

> [@ufechner7](#):
>
> The GC improved at lot in 1.10, I have no more OOM events since this version…

That’s good, I do still wonder if the Mesh allocation might be beneficial though.

[Previous page](https://discourse.julialang.org/t/julia-gc-heap-fragmentation-out-of-memory-push-append/85224.md?page=1)
