Page 1 of 1

Memory Allocation

Posted: Fri May 08, 2009 3:46 pm
by asdfgh
To speed up memory allocation wherever we start a process we can allocate a specified amount of memory using malloc during start of the program
and subsequent memory allocations smaller than the available memory is made from this block locally without having to give a system call.

while freeing memory too we make note of memory location locally.
If memory needed gets more than available memory again we allocate a block of memory and repeat this procedure.

These memory blocks are freed only while exiting the program.

This improves the execution time greatly for programs i tested

is this feasible and is my approach correct ??

Re: Memory Allocation

Posted: Sat May 09, 2009 2:08 am
by skyking
Yes, but there's some details that you may consider. First the normal way of doing memory allocations is to have a system call that allocates larger chunks of memory via syscall and then userspace code that manages these memory chunks. This reduces the number of syscalls needed to allocate memory since it's only occational that a malloc results in a syscall - the downside is that each process will consume more system memory than it really uses (but that will malloc do anyway).

Then your aproach is that there is one static chunk of memory that is allocated at start, this can be done by placing this chunk in the BSS section which gives the possibility of adjusting the amount of memory that is allocated at the start (via program loading) so that the program will only get the amount it really will allocate anyway preallocated.

Re: Memory Allocation

Posted: Tue May 12, 2009 4:46 am
by Solar
As skyking said, usually malloc() will already do something to this end. Even your average malloc() will be a quite complex beast that has been optimized by many people over a period of years, so that replacing it with some custom memory management is A) more work, B) error-prone, C) not guaranteed to perform any better.

And even if your system malloc() does not work to satisfaction, there are ready-made replacements like libhoard which you should test first, before setting out to re-implement the wheel.