This content originally appeared on DEV Community and was authored by Ashish Bailkeri
Hi Everyone, I'm going to starting a mini series of articles about Advanced C++ Programming concepts that are used in a lot of real projects. Today's topic is: Arena Allocation.
Code snippets and images posted in this article are under the MIT License
Memory management is a pain, isn't it?
When working in garbage collected languages such as Java or Go, you may be mostly free from dealing closely with memory but in languages like C and C++, memory usually causes a lot of problems, especially since you have a lot of power to manipulate it.
So what's the best allocator?
There is no number 1 best allocator in every scenario, rather, if you wanted the best allocator, the programmer is the best allocator because they know exactly what the program will do and thus know the best way to allocate memory.
Arena Allocation
Instead of allocating pointers using malloc
or new
, we can create our own allocator known as the arena allocator.
This kind of allocation involves allocating a large chunk of memory before the logic of your program executes, for example, 20 GiB of memory. Wait, hold up, this sound completely unreasonable right? Yes, it is, but the operating system knows this too, so it allows overcommitting memory.
Linux Overcommit
Mac Overcommit
Windows Overcommit
NOTE: Windows doesn't have the same ability to overcommit memory, rather large amounts of memory can be reserved and then requested
When you have your large amount memory allocated, what do you do with it?
Generally the block of memory is separated into chunks, and parts of the program may be assigned dedicated amounts of memory. In certain approaches (simpler ones), objects are allocated linearly from the block of memory.
When to use arena allocation
Each specific case cannot be listed as it's different for each program but here are some reasons I use it:
- Program run-time is short and I want fast allocation
- Create an optimized allocator that saves on performance and flexibility
Create an allocator
Here is the way I set up my arena allocator for my language Jet,
.
The way this is setup is pretty generic, it allows me to create multiple allocators for different parts of my program, which is general pretty common when using arena allocation. For example,
- Part A of Program 5 GiB
- Part B of Program 10 GiB
- Part C of Program 5 GiB
This is how the large pool of memory can be distributed and is useful if you know which part allocates more memory.
Conclusion
Arena allocation is just another tool in the box that will help you advance your knowledge of low-level programming in C++. Understanding allocators behind the scenes will help you in general for any kind of endeavor.
This content originally appeared on DEV Community and was authored by Ashish Bailkeri
Ashish Bailkeri | Sciencx (2021-10-16T22:33:56+00:00) Advanced C++: Arena Allocation. Retrieved from https://www.scien.cx/2021/10/16/advanced-c-arena-allocation/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.