tags:

views:

50

answers:

2

Hello all,

I know we could take some advantages from creating private heap of Windows especially for frequently allocated and de-allocated small chunks. But I think the normal approach is to allocate a large memory from default heap and manage the allocations and de-allocations ourselves. My question is which way is advantages and disadvantage between those two ways?

Thanks, Max

+1  A: 

Allocating larger chunks is commonly done in pool allocators, where the overhead of allocation and deallocation is reduced and locality is increased (as memory is more likely to be consecutive). More information on pool allocators

In many cases, fragmentation is your worst enemy. When you are allocating, keep object sizes consistent or in sizes (power of two is popular, but may be too wasteful). This reduces fragmentation as there are only a few common sizes of memory which are allocated.

Yann Ramin
Thanks for your reply. I think my question is which one will be better, using the memory pool or private heap?
Max
@Max: There is no correct answer. It all depends on your application and usage pattern.
Yann Ramin
+2  A: 

Some advantages of managing your own heap:

  • You might be able to optimize very specifically for your own allocation needs and improve performance.
  • You may be able to avoid the use of synchronization objects if you know the concurrency rules.
  • A single free can release an entire set of allocations. For example, a short lived process that needs a bunch of small allocations that are freed all at once could carve them out of a larger block, which can be freed with a single call later.

The disadvantages, though, are very big. The added complexity will produce more bugs, more difficult maintenance, and quite possibly poorer performance in the end. I have absolutely no data to support this, but I suspect that more home-grown heap management systems result in worse performance than help it.

Some advantages of using the system's allocations (e.g., HeapAlloc):

  • Less complexity.
  • Reduced risk of concurrency problems in the allocation/freeing.
  • The ability to take advantage of the Low-Fragmentation Heap. This already does a very good job in most cases of handling the small allocations very efficiently.
Mark Wilkins
Many thanks, Mark.
Max