Wednesday, December 12, 2007

Reducing Heap Fragmentation with Low Fragment Heap Featuer

Today most pc have enough memory . But there is still chance to occur fragmentation by continuously allocating and freeing small chunk of memory.

Windows Xp , Windows Server 2003 has a new feature called Low fragment heap(LFH).
If you enable this when creating heap , this can reduce heap fragmentation.The LFH has a predefines set of memory chunks of different sizes. it called this chunk as buckets , so when we request for a a few bytes , the LFH returns the bucket of smallest size.  

The LFH option can be enable by the API HeapSetInformation. For example it may look like
long HeapFragValue = 2;

HeapSetInformation( GetProcessHeap(),HeapCompatibilityInformation,HeapFragValue,sizeof(HeapFragValue) );

2 indicates the information is about LFH.
This feature is very useful. Otherwise you may need to write your own allocators and handle the memory pool.
When writing allocator we need to think about the page size , number of page faults etc. and certainly it takes some time.   So if you are in hurry try the LFH option. :)

No comments: