I only discovered today that both the debug allocator (gpa) and the smp allocator indefinitely caches small/medium allocations. Firstly is this correct?
If the above is correct, what would a good way be to have some global backing allocator(s) for my http request based arena allocators. However the backing allocator(s) should periodically (merge and?) return pages to the os, preventing bursts of allocations indefinitely keeping that memory cached.
The first solution i could think of was having two global debug/smp allocators that the program atomically toggles between every hour, at which point the one being toggled to, is first deinitialized, starting it from a clean slate. Obviously I would not use these for memory allocations that need to live longer than an hour (e.g. postgres pool of clients).
Thanks for any help in advance.
1 Like
Debug allocator returns pages as soon as possible and avoids reusing them, this makes it much more likely a use after free crashes your program.
smp certainly reuses memory using whatever algorithm it does, I can’t remember.
There is std.heap.page_allocator, but this always uses pages even if you request smaller amounts of memory, so it is best used to back other allocators (it is often the default).
I am confused on what behaviour you are asking for exactly?
perhaps this helps Documentation - The Zig Programming Language
Sorry I guess my request is very confusing. To really simplify it, I want the most performant thread safe allocator that smartly caches pages from the OS, but not indefinitely, resulting in optimal cpu/memory balance.
the closest fit would be smp_allocator, or if your linking libc use the c_allocator
But what is most optimal is specific to your usecase, and AFAIK neither of those allocators allow you to configure that.
regardless, for development DebugAllocator is good to help catch bugs, though it might be too slow for your program.
1 Like
Thanks for confirming this, helps a lot with my mental model of these allocators. The usecase is a web server so still using ArenaAllocator on each request, but would want its backing allocator to also cache pages from the os. Think I will just stick with the smp allocator in production until I see the memory usage causing problems.