囤积内存管理器

时间:2014-07-25 12:01:01

标签: delphi delphi-xe6

如何在Delphi中使用Hoard Memory Manager?我正在寻找FastMM的替代方案,这对于严肃的多线程服务器应用程序来说是没有希望的。我看了ScaleMM2,但它在64位时不稳定。

如何静态链接Hoard Memory Manager。因为它带有OBJ文件。

3 个答案:

答案 0 :(得分:4)

你无法真实地希望静态链接Hoard,因为它是用C ++实现的,因此需要一个C ++运行时。您不能指望在Delphi程序中放置C ++运行时。

因此,您需要在DLL中构建Hoard内存管理器,例如使用Visual Studio。该DLL必须导出mallocfreerealloc。使用.def文件执行此操作。然后创建一个简单的Delphi单元,链接到DLL并根据这些导入的函数安装内存管理器。使用我对other question的回答来指导您。与往常一样,使内存管理器单元成为.dpr文件的使用列表中的第一个单元。

请确保您遵守许可。如果我没记错的话,您需要支付商业许可证,或者根据GPL许可您的软件。

FWIW,我使用malloc中系统C运行时的msvcrt.dll,它比FastMM更好地扩展。

答案 1 :(得分:4)

正如大卫所说,你需要一个.dll版本。如果你不能自己构建它,你可以找到预先构建的版本。仅限快速搜索turned up an older version, though

AndréMusschemodified the FastCode MM Challenge在2011年添加Hoard。如果您browse the source code,您将看到如何使用它的示例,包括另一个winhoard.dll的预编译副本。但是,performancememory usage在这些测试中效果不佳。

答案 2 :(得分:-2)

由于每个人都 FAILED 提供详细的解决方案。这是一个真正有效的解决方案。

1:打开libhoard.cpp 2:在代码中添加以下行。

extern "C"{

__declspec(dllexport) void* scalable_malloc (size_t size)
{
    return malloc(size);
}

__declspec(dllexport) void* scalable_realloc (void* ptr, size_t size)
{
    return realloc(ptr, size);
}

__declspec(dllexport) void scalable_free (void* ptr)
{
    free(ptr);
}

}

3:使用nmake windows

进行编译

4:使用它

unit hoard_mm;

interface

implementation

type
  size_t = Cardinal;

const
  hoardDLL = 'libhoard.dll';

function scalable_malloc(Size: size_t): Pointer; cdecl; external hoardDLL;
function scalable_realloc(P: Pointer; Size: size_t): Pointer; cdecl; external hoardDLL;
procedure scalable_free(P: Pointer); cdecl; external hoardDLL;

function GetMem(Size: NativeInt): Pointer; inline;
begin
  Result := scalable_malloc(size);
end;

function FreeMem(P: Pointer): Integer; inline;
begin
  scalable_free(P);
  Result := 0;
end;

function ReallocMem(P: Pointer; Size: NativeInt): Pointer; inline;
begin
  Result := scalable_realloc(P, Size);
end;

function AllocMem(Size: NativeInt): Pointer; inline;
begin
  Result := GetMem(Size);
  if Assigned(Result) then
  FillChar(Result^, Size, 0);
end;

function RegisterUnregisterExpectedMemoryLeak(P: Pointer): Boolean; inline;
begin
  Result := False;
end;

const
  MemoryManager: TMemoryManagerEx = (
    GetMem: GetMem;
    FreeMem: FreeMem;
    ReallocMem: ReallocMem;
    AllocMem: AllocMem;
    RegisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak;
    UnregisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak
  );

initialization
  SetMemoryManager(MemoryManager);

end.