Why create a custom memory manager?
To appreciate how the control over memory allocation helps your code run faster, first recollect the basics of memory management in C/C++. The standard library functions
realloc in C and the
new [ ],
delete [ ] operators in C++ form the crux of the memory management in these two languages. With this in mind, there are several things worth your attention here.
Functions such as
new are general-purpose memory allocators. Your code may be single-threaded, but the
malloc function it is linked to can handle multithreaded paradigms just as well. It is this extra functionality that degrades the performance of these routines.
In their turn,
new make calls to the operating system kernel requesting memory, while
delete make requests to release memory. This means that the operating system has to switch between user-space code and kernel code every time a request for memory is made. Programs making repeated calls to
new eventually run slowly because of the repeated context switching.
Memory that is allocated in a program and subsequently not needed is often unintentionally left undeleted, and C/C++ don't provide for automatic garbage collection. This causes the memory footprint of the program to increase. In the case of really big programs, performance takes a severe hit because available memory becomes increasingly scarce and hard-disk accesses are time intensive.