|
Dynamic Memory Allocation Performance |
|
|
|
|
Written by SVTechie
|
|
Sunday, 09 April 2006 |
|
Page 2 of 7
ANSI-C Basic Memory Manipulation Function Review
- malloc - The standard library function is commonly used to allocate memory. Malloc returns a void pointer to the allocated buffer.
This pointer must be cast into the proper type to access the data to
be stored in the buffer.
- free -
When using dynamically allocated memory, it is necessary for the programmer to free the
memory after its use.
C++ Memory Manipulation Functions
new/delete are standard C++ functions to perform dynamic memory allocation. These are very easy to use and provides better readability compared to C counterpart functions (malloc/free). But performance loss is significant if C++ functions are used. Comparison is shown later.
Simple Record Collapsing Scheme
This mechanism is collapsing similar data types in one big array and in turn, save number of allocation/de-allocation calls to operating system. This has to be done at the time of software architecture development. A typical programming is shown and used to show performance gain. Actually this one is the most efficient of all schemes and really emphasizes the fact that performance gain resulting from good architecture normally outweighs lower level tweaks. However, It may not be feasible to use this scheme in real systems where significant amount of development is already done. Also sometime application itself may require more flexibility.
Library Function for Pool Based Memory Allocation Scheme
This mechanism is independent of data type and a simple function library implementation is provided to show performance gain/loss. Basic idea is to allocate big chunk of memory in a function which is equivalent to malloc (malloceq ) but functionally is super set of malloc. Whenever malloceq function call is performed, memory is assigned from already allocated memory pool and when there is no space left in the memory pool, new memory pool is allocated. Aim is to explain Pool Based memory allocation scheme by example and example function library (malloceq/freeeq) is implemented in ANSI C and usage is shown in this article. Also please note that implementation at lower level library will result in better performance gain.
Dynamic Memory Allocation Problems
Dynamic allocation/de-allocation creates lots of problems as indicated:
-
Poor performance. Deallocation of a large data set, one element at a time, can be very time consuming and
can have a large impact on program performance. This can be avoided
using a block based memory allocator, where the elements
are allocated from large memory blocks.
-
Memory leaks. A memory leak takes place when memory is allocated but
never deallocated. Tools like
purify or Boundchecker can be used to track and fix this issue.
-
Pointers to deallocated memory. When memory is deallocated there may
still be pointers in use to the deallocated memory. This may cause unexpected behavior at run time.
Even carefully crafted code written by experienced software engineers
tends to suffer from problems with memory leaks and references to
deallocated memory.
|
|
Last Updated ( Saturday, 06 May 2006 )
|