Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell the MS CRT to use a Low Fragmentation Heap on Windows XP?

Tags:

It appears that the MS CRT (msvcr80.dll from VS2005 in my case) uses a heap different from the standard process heap returned by GetProcessHeap().

On Windows XP, the default for a heap created with HeapCreate is non-low-fragmentation. How do I tell the CRT to use a Low Fragmentation Heap instead?

like image 979
Martin Ba Avatar asked Dec 02 '25 09:12

Martin Ba


1 Answers

See the example here: _get_heap_handle :

intptr_t _get_heap_handle( void );

Returns the handle to the Win32 heap used by the C run-time system.

Use this function if you want to call HeapSetInformation and enable the Low Fragmentation Heap on the CRT heap.

// crt_get_heap_handle.cpp
// compile with: /MT
#include <windows.h>
#include <malloc.h>
#include <stdio.h>

int main(void)
{
    intptr_t hCrtHeap = _get_heap_handle();
    ULONG ulEnableLFH = 2;
    if (HeapSetInformation((PVOID)hCrtHeap,
                           HeapCompatibilityInformation,
                           &ulEnableLFH, sizeof(ulEnableLFH)))
        puts("Enabling Low Fragmentation Heap succeeded");
    else
        puts("Enabling Low Fragmentation Heap failed");
    return 0;
}
like image 153
Martin Ba Avatar answered Dec 04 '25 16:12

Martin Ba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!