views:

108

answers:

2

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

  1. nedmalloc- Did not pass the 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.

A: 

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)

Necrolis
Thanks.I think nedmalloc is implemented based on the doug lea's malloc (dlmalloc). dlmalloc is not a drop-in replacement I'm looking for previously. For tracking the problem of nedmalloc crash (int5), I'm not sure. It happened in high stress testing randomly.
Jun Wan
yeah, nedmalloc is based around dlmalloc as its core allocator, however that doesn't mean dlmalloc is the source of the problems. according to the header though dlmalloc is pretty much a drop in replacement (just note the multithreading warning, use ptmalloc if your after multithreading)
Necrolis
A: 

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;
}
Steve Townsend
Thanks. I've tried it and was forced to abandon. Actually, it's not possible for me to use LFH. Work with millions of legacy code and cost to much to re-do them with LFH. Moreover, it's windows only.
Jun Wan