Click here to Skip to main content
15,351,281 members
Articles / Desktop Programming / MFC
Posted 14 Jan 2004


16 bookmarked

Low Fragmentation Heap and Function Interception

Rate me:
Please Sign up or sign in to vote.
2.50/5 (8 votes)
14 Jan 20042 min read
How to implement a low fragmentation heap by intercepting HeapAlloc.


This article shows how to implement the Low Fragmentation Heap available in Windows 2003 Server/ XP and Windows 2000 SP5.

I originally posted this article yesterday but then discovered Detours so I've re-posted it.


The project makes use of the Detours project available from Detours from which you can download the source. I have already done this and repackaged it into a project which you can build using the IDE as the original project uses NMake.

The essential elements are that the LFH.Dll is loaded by a call to SetLFH. During processing the attached DLL modifies the calling program's code to intercept the calls to HeapAlloc so that all the calls are now processed by the LFH.Dll version of this function.

I'm not going to explain anything about Detours as everything you need can be downloaded from the link given above.

There's not very much to this program really as all the meat and potatoes is performed by Detours. All LFH does is maintain a linked list of heap handles for which LFH is set on and optimize the memory block size for each allocation to be a multiple of 8, 16, 32, 64, 128, 512.

Using the Code

Include the header allocator.h in the stdafx.h file and add LFH.Lib to the linker input. Two TestHarness sample programs are supplied.

// Set LFH on for this processes heap
SetLFH( GetProcessHeap(), true);

// Allocate using new allocators
pTest = new CTest;


I would expect this to work on versions of Windows, pre-Win2K but I've not tested it. If you do not have Visual Studio 2003 then you need to create a Windows DLL that exports symbols and copy allocator.h, allocator.cpp to your newly created project. You will have to do the same for Detours or use the code from the Microsoft download link above.

Detours also opens up a range of program instrumentation possibilities as it's very easy to intercept any function call and produce diagnostics, tracing, performance statistics.

If you find any bugs please let me know (nicely please). My code is just as good as anybody else's... It has bugs too...


  • 15th Jan, 2005 - Article posted.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Austria Austria
C++/C# developer (MSCD)

Comments and Discussions

QuestionLFH look-a-like? Pin
kpinkert3-Aug-05 9:23
Memberkpinkert3-Aug-05 9:23 
AnswerRe: LFH look-a-like? Pin
bobsheep3-Aug-05 18:01
Memberbobsheep3-Aug-05 18:01 
QuestionWhy is this recreated again? Pin
dog_spawn15-Jan-04 7:55
Memberdog_spawn15-Jan-04 7:55 
AnswerRe: Why is this recreated again? Pin
Anonymous15-Jan-04 9:39
MemberAnonymous15-Jan-04 9:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.