How to convince the memory manager to release unused memory

后端 未结 4 1387
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 07:32

In a recent post ( My program never releases the memory back. Why? ) I show that when using FastMM, the application does not release substantial amounts of memory back to th

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 07:49

    I use the following as a memory manager. I do so because it performs much better under thread contention than FastMM which is actually rather poor. I know that a scalable manager such as Hoard would be better, but this is works fine for my needs.

    unit msvcrtMM;
    
    interface
    
    implementation
    
    type
      size_t = Cardinal;
    
    const
      msvcrtDLL = 'msvcrt.dll';
    
    function malloc(Size: size_t): Pointer; cdecl; external msvcrtDLL;
    function realloc(P: Pointer; Size: size_t): Pointer; cdecl; external msvcrtDLL;
    procedure free(P: Pointer); cdecl; external msvcrtDLL;
    
    function GetMem(Size: Integer): Pointer;
    begin
      Result := malloc(size);
    end;
    
    function FreeMem(P: Pointer): Integer;
    begin
      free(P);
      Result := 0;
    end;
    
    function ReallocMem(P: Pointer; Size: Integer): Pointer;
    begin
      Result := realloc(P, Size);
    end;
    
    function AllocMem(Size: Cardinal): Pointer;
    begin
      Result := GetMem(Size);
      if Assigned(Result) then begin
        FillChar(Result^, Size, 0);
      end;
    end;
    
    function RegisterUnregisterExpectedMemoryLeak(P: Pointer): Boolean;
    begin
      Result := False;
    end;
    
    const
      MemoryManager: TMemoryManagerEx = (
        GetMem: GetMem;
        FreeMem: FreeMem;
        ReallocMem: ReallocMem;
        AllocMem: AllocMem;
        RegisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak;
        UnregisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak
      );
    
    initialization
      SetMemoryManager(MemoryManager);
    
    end.
    

    This isn't an answer to your question, but it's too long to fit into a comment and you may find it interesting to run your app against this MM. My guess is that it will perform the same way as FastMM.

提交回复
热议问题