How to convince the memory manager to release unused memory

后端 未结 4 1362
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  • 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.

    0 讨论(0)
  • 2020-12-18 07:58

    SOLVED

    As suggested by Barry Kelly the memory will be released automatically by FastaMM. To confirm that this I crated a second program that allocated A LOT of RAM. As soon as Windows ran out of RAM, my program memory utilization returned to its original value.

    Problem solved. Thanks Barry.

    0 讨论(0)
  • 2020-12-18 08:00

    You shouldn't think about it as "wasting" RAM, really. Think about it as "caching" unused RAM. The memory manager is holding onto the unused memory instead of releasing it back to the OS for a reason, and in fact you've hit upon that reason in your question.

    You said that you keep re-running the same operations in a loop. When you do that, it still has the old memory available and it can assign it immediately, instead of having to ask Windows for a new chunk of heap. This is one of the tricks that puts the "Fast" in "FastMM," and if it didn't do that you'd find your program running a lot more slowly.

    You don't need to worry about the FastMM debug mode figure. That's only for debugging, and you're not going to release a program compiled against FullDebugMode. And the difference between "without FastMM" and "with FastMM Release Mode" is about 1 MB, which is negligible on modern hardware. For the low cost of only 1 extra MB, you get a big performance boost. So don't worry about it.

    0 讨论(0)
  • 2020-12-18 08:03

    Part of what makes FastMM fast is that it will allocate a large block of memory and carve smaller uniformly sized pieces out of it. If any part of the block is in use, none of it can be released back to the OS.

    You're welcome to use a different memory manager. One approach would be to route all allocations directly to VirtualAlloc. Allocations will be rounded up to occupy an entire page at a time, so your program may suffer if you have lots of small allocations, but when you call VirtualFree, you can be confident that the memory definitely doesn't belong to your program anymore.

    Another option is to route everything to the OS heap. Use HeapAlloc. You can even enable the low-fragmentation heap for your program (on by default as of Windows Vista), which will make the OS employ a strategy similar to the one used by FastMM, but it will allow you to use some debugging and analysis tools from Microsoft to track your program's memory usage over time. Beware, though, that after you call HeapFree, some metrics might still show the memory as belonging to your program.

    Besides, the working set refers to the memory that's currently in physical RAM. That you observed the number go up does not mean that your program has allocated any more memory. It can simply mean that your program touched some memory that it had previously allocated, but which had not yet been put into RAM. During your loop, you touched that memory, and the OS has not decided to page it back out to disk yet.

    0 讨论(0)
提交回复
热议问题