views:

328

answers:

4

Windows API has a set of function for heap creation and handling: HeapCreate, HeapAlloc, HeapDestroy and etc. I wonder what is the use for another heap in a program? From fragmentation point of view, you will get external fragmentation where memory is not reused among heaps. So even if low-fragmentation heaps are used, stil there is a fragmentation. Memory management of additional heaps seems to be low-level. So they are not easy to use. In addition, additional heap can probably be emulated using allocations from heap and managing allocated memory.

So what is the usage? Did you use it?

+3  A: 

One use case might be a long-running complex process that does a lot of memory allocation and deallocation. If the user wants to interrupt the process, then an easy way to clean up the memory currently allocated might be to have everything on a private heap and then simply destroy the heap.

I have seen this technique used in an embedded system (which wasn't using Windows, so it didn't use those exact API functions). The custom memory allocator had a feature to "mark" a specific state of the heap and then "rewind" to that point if a process was aborted.

Greg Hewgill
+2  A: 

Use: Very very very rarily.

Usage:
I once worked on a projected that used the heap management as a crude garbage collector (no destructors). There was a section of the code that went off a did some work without regard to memory management (using a seprate heap). Then when it was done we jsut destroyed that heap to re-claim all the memory.

Martin York
A: 

You might also dedicate a heap per thread - for locality of reference or to reduce locking (which is required when a heap is shared across threads).

sean e
A: 

One use is for fixed size objects. If you need to do a lot of allocation/deallocation of objects that are all the same size (i.e. small message buffers) a private heap avoids fragmentation issues.

Steve Fallows
ie you use the low-fragmenetation heap, which is also much faster.
gbjbaanb