Memory Allocation

Programming, for all ages and all languages.
Post Reply
asdfgh
Member
Member
Posts: 42
Joined: Fri Apr 18, 2008 9:14 pm

Memory Allocation

Post 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 ??
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: Memory Allocation

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Memory Allocation

Post 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.
Every good solution is obvious once you've found it.
Post Reply