Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Right now I'm trying to create a Heap that requires 8-byte alignment, and was looking around online for some good methods that would minimize the amount of fragmentation. Less fragmentation is ideal, and even the ones that will slow down a lot are a consideration (but speed is also a sub-goal here)

Has anyone had any experience with Heaps who could let me know what to watch out for when going about this? Any input is appreciated

share|improve this question
Heap like in "";? A binary max heap, for example, does never get fragmented. So what the heck are you talking of? – Doc Brown Oct 11 '11 at 18:29
@DocBrown: Looking at the mention of alignment and OP's previous questions, my guess is "heap as in dynamic memory allocation". – delnan Oct 11 '11 at 19:26
Yup, that is correct! – Dark Templar Oct 12 '11 at 1:24
up vote 2 down vote accepted

A coalescing heap will alleviate (but not avoid) fragmentation by coalescing adjacent free blocks. Whenever you free a block, look at the next and previous blocks in your list. If either (or possibly both) of them are free, then you can combine them to form one large block. Some implementations defer the coalescing until allocation fails, at which point the allocator will walk the heap and look for adjacent free blocks to merge until it gets one large enough to satisfy the request. This gives better average-case performance, but worst-case performance is much worse. Merging blocks at deallocation time amortizes the cost over all deallocations, so there's not much variance between best-case and worst-case performance.

share|improve this answer
Hmm... I still don't understand what you would mean by 'best-case', 'average' and worst? – Dark Templar Oct 12 '11 at 1:29
If you don't merge the blocks on deallocation, then your deallocation call returns faster, giving you better performance. Worst-case performance takes a hit because an allocation may walk the entire heap and find no blocks large enough to satisfy the request. So now the allocator has to walk the heap again, looking for adjacent free blocks to merge until it is able to create one large enough to satisfy the request. If you merge at deallocation, then you never have to walk the heap more than once. – TMN Oct 12 '11 at 12:10
Hmm I see. Overall though, does it save more time (cumulatively) to do the deallocate-when-allocation-fails technique? – Dark Templar Oct 12 '11 at 17:25
It depends on your usage patterns and implementation. Merging on allocation hurts when your heap gets badly fragmented, so you wind up merging several small blocks. You can take a hybrid approach and make a "merge pass" every N allocations (say every 1000) or whenever some condition is met (like over N% of the heap is allocated, or there are over N separate free blocks). If this is for a general-purpose allocator, I'd recommend having some kind of tuning parameter, so you can specify which strategy to use. – TMN Oct 12 '11 at 18:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.