Can anyone point me to a source or outline how the algorithm for the low-fragmentation heap works?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 How to solve Memory Fragmentation, and my comment at Memory management in memory intensive application