Memory Allocator

Today I want to describe a very simple memory allocator – memory pool with fixed block size. Memory allocators are important especially for embedded systems. Such systems often require a memory management module which help to decrease memory fragmentation and provide efficient memory usage.

The idea for a simple memory management module is to allocate memory once and use it frequently. Memory allocation algorithm shouldn’t allocate any memory block outside the pool. It is possible to create more than one memory pool, e.g one memory pool for each queue.

The full code of the memory allocator is provided on my GITHub profile in CWorking repository. Here I would like to show the result of memory allocation algorithm. Sample application allocates pool for 10000, 4 byte memory blocks. Each block  is occupied in a loop by an integer number. Finally the memory blocks are tested, released and the whole pool is removed.

Cworking_Memory_Allocator Massif

As you can see above, memory is allocated only at the beginning. During the program, the pool is used frequently and finally the whole memory is released. This confirms that this memory allocator might be a good solution for the systems where a low fragmentation is required e.g for embedded systems. Moreover, such algorithm helps to protect our system against the memory leaks after the unexpected crash. I encourage you to download the code and try to understand it and maybe optimize the implemented algorithm.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s