Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the term arena in relation to memory?

I'm reading a book on memory as a programming concept. In one of the later chapters, the author makes heavy use of the word arena, but never defines it. I've searched for the meaning of the word and how it relates to memory, and found nothing. Here are a few contexts in which the author uses the term:

"The next example of serialization incorporates a strategy called memory allocation from a specific arena."

"...this is useful when dealing with memory leaks or when allocating from a specific arena."

"...if we want to deallocate the memory then we will deallocate the whole arena."

The author uses the term over 100 times in one chapter. The only definition in the glossary is:

allocation from arena - Technique of allocating an arena first and then managing the allocation/deallocation within the arena by the program itself (rather then by the process memory manager); used for compaction and serialization of complex data structures and objects, or for managing memory in safety-critical and /or fault-tolerant systems.

Can anyone define arena for me given these contexts?

like image 499
Nocturno Avatar asked Oct 15 '22 15:10

Nocturno


People also ask

What is Arena allocation?

With arena allocation, new objects are allocated out of a large piece of preallocated memory called the arena. Objects can all be freed at once by discarding the entire arena, ideally without running destructors of any contained object (though an arena can still maintain a "destructor list" when required).

What is a linear allocator?

Linear Allocation As the name suggests, memory is allocated linearly. Throughout this series, I will be using the concept of an allocator as a means to allocate this memory. A linear allocator, is also known by other names such as an Arena or Region-based allocator.


2 Answers

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. For example:

char * arena = malloc(HUGE_NUMBER);

unsigned int current = 0;

void * my_malloc(size_t n) { current += n; return arena + current - n; }

The point is that you get full control over how the memory allocation works. The only thing outside your control is the single library call for the initial allocation.

One popular use case is where each arena is only used to allocate memory blocks of one single, fixed size. In that case, you can write very efficient reclamation algorithms. Another use case is to have one arena per "task", and when you're done with the task, you can free the entire arena in one go and don't need to worry about tracking individual deallocations.

Each of those techniques is very specialized and generally only comes in handy if you know exactly what you're doing and why the normal library allocation is not good enough. Note that a good memory allocator will already do lots of magic itself, and you need a decent amount of evidence that that's not good enough before you start handling memory yourself.

like image 163
Kerrek SB Avatar answered Nov 03 '22 09:11

Kerrek SB


I'll go with this one as a possible answer.

•Memory Arena (also known as break space)--the area where dynamic runtime memory is stored. The memory arena consists of the heap and unused memory. The heap is where all user-allocated memory is located. The heap grows up from a lower memory address to a higher memory address.

I'll add Wikipedia's synonyms: region, zone, arena, area, or memory context.

Basically it's memory you get from the OS, and divvy out, then can be freed all at once. The advantage to this is that repeated small calls to malloc() could be costly (Every memory allocation has a performance cost: the time it takes to allocate the memory in your program’s logical address space and the time it takes to assign that address space to physical memory) where as if you know a ball park you can get yourself a big chunk of memory then hand it out to your variables as/how you need it.

like image 36
Mike Avatar answered Nov 03 '22 10:11

Mike