开发者

Windows tcmalloc replacement with static linking

开发者 https://www.devze.com 2023-01-18 01:55 出处:网络
A C++ project encounter the memory fragmentation problem, and tried following: nedmalloc- Did not pass t开发者_运维问答he stress test (crashed after 15 hrs), that means it works in the most of cases

A C++ project encounter the memory fragmentation problem, and tried following:

  1. nedmalloc- Did not pass t开发者_运维问答he stress test (crashed after 15 hrs), that means it works in the most of cases but not the all. And more memory usage than other allocators.

  2. jemalloc- Not ready for Windows?

  3. tcmalloc- Compiled with host code with static linking, but conflict with the CRT symbols. Can I just use the alias like tc_malloc(...) to build my own wrapper for allocation? How to do that?

Any comments? Thanks in advance.


Set up your project to use the Windows Low Fragmentation Heap (LFH) using this API at the start of the program. This may fix your problem without more work on custom implementations.

Sample code, taken directly from MSDN:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#define HEAP_LFH 2

int __cdecl _tmain()
{
    BOOL bResult;
    HANDLE hHeap;
    ULONG HeapInformation;

    // 
    // Note: The HeapSetInformation function is available on Windows 2000 with SP4
    // only if hotfix KB 816542 is installed. To run this example on Windows 2000,
    // use GetProcAddress to get a pointer to the function if available.
    //

    //
    // Enable heap terminate-on-corruption. 
    // A correct application can continue to run even if this call fails, 
    // so it is safe to ignore the return value and call the function as follows:
    // (void)HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
    // If the application requires heap terminate-on-corruption to be enabled, 
    // check the return value and exit on failure as shown in this example.
    //
    bResult = HeapSetInformation(NULL,
                                 HeapEnableTerminationOnCorruption,
                                 NULL,
                                 0);

    if (bResult != FALSE) {
        _tprintf(TEXT("Heap terminate-on-corruption has been enabled.\n"));
    }
    else {
        _tprintf(TEXT("Failed to enable heap terminate-on-corruption with LastError %d.\n"),
                 GetLastError());
        return 1;
    }

    //
    // Create a new heap with default parameters.
    //
    hHeap = HeapCreate(0, 0, 0);
    if (hHeap == NULL) {
        _tprintf(TEXT("Failed to create a new heap with LastError %d.\n"),
                 GetLastError());
        return 1;
    }

    //
    // Enable the low-fragmenation heap (LFH). Starting with Windows Vista, 
    // the LFH is enabled by default but this call does not cause an error.
    //
    HeapInformation = HEAP_LFH;
    bResult = HeapSetInformation(hHeap,
                                 HeapCompatibilityInformation,
                                 &HeapInformation,
                                 sizeof(HeapInformation));
    if (bResult != FALSE) {
        _tprintf(TEXT("The low-fragmentation heap has been enabled.\n"));
    }
    else {
        _tprintf(TEXT("Failed to enable the low-fragmentation heap with LastError %d.\n"),
                 GetLastError());
        return 1;
    }

    return 0;
}


There is the project of jemalloc for visual studio 2008, 2010, and 2013. https://github.com/shines77/jemalloc-win32

I suppose that it implies jemalloc can be used in windows.

But I've not tried it yet. Check it out yourself.


there are a few other allocators available too, like doug lea's malloc (dlmalloc) or the horde allocator


nedmalloc- Did not pass the stress test (crashed after 15 hrs), that means it works in the most of cases but not the all.

see if you can track down where and why it crashed first before abbandoning it, it might just be an error from your side, also check ned's SVN repo, there might already be a fix for your problem.

tcmalloc- Compiled with host code with static linking, but conflict with the CRT symbols. Can I just use the alias like tc_malloc(...) to build my own wrapper for allocation? How to do that?

I'd say its best to keep the CRT symbols intact(just in case), so go ahead and edit to project so that the confilicating symbols instead follow a convention you desire (you have the source for a reason after all)


Intel's TBB provides an allocator that may work. http://threadingbuildingblocks.org/download.php

0

精彩评论

暂无评论...
验证码 换一张
取 消