# Memory Arena

**URL:** <https://discourse.julialang.org/t/memory-arena/123764>\
**Category:** Performance\
**Tags:** question, c\
**Created:** [December 12, 2024, 3:13pm UTC](https://discourse.julialang.org/t/memory-arena/123764 "2024-12-12T15:13:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 12, 2024, 3:13pm UTC](https://discourse.julialang.org/t/memory-arena/123764/1 "2024-12-12T15:13:08Z")

</div>

Hello, dear members of the Julia Community.  
**I want to manage heap space for external objects (e.g. C objects) by using an Arena**  
**How is it done in Julia?** The simple definition of an Arena is _“An arena is just a large, contiguous piece of memory that you allocate once and then use to manage memory manually by handing out parts of that memory.”_  
Please **do not** suggest any external libraries.  
To name one of its benefits. _ **“To avoid fragmenting memory.”** _

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [December 12, 2024, 4:57pm UTC](https://discourse.julialang.org/t/memory-arena/123764/2 "2024-12-12T16:57:30Z")

</div>

> Please **do not** suggest any external libraries.

This suggests that you want to learn how to implement a simple arena allocator in julia. The short answer is “the obvious way, nothing julia specific here”.

Since you are asking the question anyway, I assume this is not obvious to you. Props to you for learning something new, don’t get annoyed when I write something that is in fact obvious to you.

```julia
julia> mutable struct Arena
       ptr::Ptr{Cvoid}
       len::Int
       cap::Int
       end
julia> function makeArena(cap::Int)
       cap == 0 && return Arena(Base.C_NULL, 0, 0)
       Arena(ccall(:malloc, Ptr{Cvoid}, (Csize_t,), cap), 0, cap)
       end
julia> function freeArena(a::Arena)
       ccall(:free, Cvoid, (Ptr{Cvoid},), a.ptr)
       a.ptr = C_NULL
       a.len = 0
       a.cap = 0
       nothing
       end
julia> function allocFromArena(a::Arena, size)
       a.len + size > a.cap && return C_NULL
       res = a.ptr + a.len
       a.len += size
       res
       end

```

And that’s it. You can make an arena, tear it down, and allocate memory from it. I even introduced some minor safety features to make double-free and use-after-free of the arena more benign.

From that, there are lots of ways you can go. Thread-safety via atomics. Convenience methods for using the arena as a stack. Methods to get aligned allocations. Avoid memory leaks by letting julia GC take over the freeing via finalizers. Whatever floats your boat.
