Why does my Delphi program's memory continue to grow?

后端 未结 4 1191
北海茫月
北海茫月 2020-12-09 11:31

I am using Delphi 2009 which has the FastMM4 memory manager built into it.

My program reads in and processes a large dataset. All memory is freed correctly whenever

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 12:26

    You could use VMMap to trace the most allocated bytes. It helped me for an similar scenario.

    • Download VMMap
    • Compile your application with map file detailed
    • Convert the map file to dbg, to VMMap understand it. Use the map2dbg tool
    • Configure symbol (dbg) path on VMMap: Options -> Configure Symbols -> Symbol paths
    • Configure source paths on VMMap -> Options -> Configure Symbols -> Source code paths. Hint: use the "*" to include subfolders
    • In VMMap, go to File -> Select Process -> Launch and trace a new process. Configure application and any parameter that it needs. Then Ok.

    When the app opens, VMMap will trace all allocated and freed memory using detours in allocate/free methods. You can see in Timeline button (on the botton of VMMap) the timeline of memory (obviously).

    Click in Trace button. It will show all the allocations/dealocattions operations in the traced time. Order the column Bytes to show the most bytes first, and double click it. It will show the callstack of the allocation. In my case, the first item showed my problem.

    Sample app:

    private
      FList: TObjectList;
      ...
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i: Integer;
    begin
      for i := 0 to 1000000 do
        FList.Add(TStringList.Create);
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      a: TStringList;
    begin
      FList := TObjectList.Create; //not leak
      a := TStringList.Create; //leak
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      FList.Free;
    end;
    

    When clicking in button one time, and see the Trace in VMMap, shows:

    And the callstack:

    In this case did not show exactly the code, but the Vcl.Controls.TControl.Click give an idea. In my real scenario, helped more.

    There is a lot of others functionalities in VMMap that helps analising memory problems.

提交回复
热议问题