Hey,
Can anyone point me to a source or outline how the algorithm for the low-fragmentation heap works?
Thanks in advance
Hey,
Can anyone point me to a source or outline how the algorithm for the low-fragmentation heap works?
Thanks in advance
First decide which 'multiples' you want to use for the allocated memory chunks. I typically use 8 bytes.
At startup of the application, make a vector where each element in the vector points to a 'pool' of memory chunks. The first index in the vector will be for memory allocations of 8 bytes or less. The second index in the vector will be for memory allocations from 9-16 bytes, and so on.
Within every pool, allocate memory in bigger chunks. E.g. in the pool for allocations of 8 bytes (or less), don't allocate 8 bytes, but allocate N times 8 bytes. Within the pool remember which parts are really allocated in the application and which parts are just waiting to be allocated.
When freeing memory, don't free it immediately, but keep some chunks ready for the next allocation of that size. Only if you have a lot of subsequent free chunks, return the free memory to the operating system.
That's the basic idea. The rest is implementing the pools (typically linked lists of chunks).
The difficult part is integrating the heap implementation into the application.
Also take a look at http://stackoverflow.com/questions/60871/how-to-solve-memory-fragmentation, and my comment at http://stackoverflow.com/questions/473958/memory-management-in-memory-intensive-application/2168539#2168539