Memory consumption be a specific object lazarus / 21-General ---------------------------------------------------------------------- stoffman · 2021-02-12 19:44 Hi, I would like to monitor how much memory a specific object instance is consuming during the lifetime of the application. By that I mean 2 things: 1. The memory being used by the object instance and everything it points to 2. I Can make it write the number every 1 minute Any idea on how to accomplish that? Thanks, ---------------------------------------------------------------------- howardpc · 2021-02-12 22:24 1 The instance size is information TObject holds. The other sizes you have to calculate manually. A simple example follows. Note this does not calculate the metadata size of the managed types, which should be added, but I don't know if FPC has functions that give that information, or if you have to work it out by hacking. 2 Use a timer? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  Types; type   TTest = class(TObject)  private    int: Integer;    arr: TIntegerDynArray;    s:   AnsiString;    function GetArrSize: Integer;    function GetStringSize: Integer;    function GetIntSize: Integer;  public    constructor Create(anInt: Integer; anArray: TIntegerDynArray; const aText: String);    function GetMemSize(out instance, integr, arry, str: Integer): Integer;  end; { TTest } constructor TTest.Create(anInt: Integer; anArray: TIntegerDynArray; const aText: String);begin  int := anInt;  arr := anArray;  s   := aText;end; function TTest.GetArrSize: Integer;begin  Exit(Length(arr) * Sizeof(Integer));end; function TTest.GetIntSize: Integer;begin  Exit(SizeOf(Integer));end; function TTest.GetMemSize(out instance, integr, arry, str: Integer): Integer;begin  instance := InstanceSize;  integr := GetIntSize;  arry := GetArrSize;  str := GetStringSize;  Result := instance + integr + arry + str;end; function TTest.GetStringSize: Integer;begin  Exit(Length(s));end; var  a: TIntegerDynArray = (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);  t: TTest;  ins, i, ay, s: Integer; begin  t := TTest.Create(100, a, 'any test string will do');  Write('GetMemSize returns ',t.GetMemSize(ins, i, ay, s));  WriteLn(' made up of instance:',ins,', integer:',i,', array:',ay,', string:',s);  t.Free;  ReadLn;end. ---------------------------------------------------------------------- stoffman · 2021-02-13 16:07 Thank you. I was looking for a solution that is less manual... ---------------------------------------------------------------------- Martin_fr · 2021-02-17 14:53 To the best of my knowledge there are no pre-build tools for this. Not even anything remotely useful.... You can write your own mem manager (wrapper). But then you still need to know for each call, if it is to be counted or not. The wrapper is easy-ish. The count-or-not is hard. Especially if maybe memory is added to the object from the outside (like adding to a list / setting a property). And where to count strings, that are hold by the object, but may well be hold outside too. Its the same mem, used twice. If you only count mem where the call to mem-alloc is done while inside (include nested and callbac) a method of the object, => on each method of the object set a flag on entry and clear it on exit => write a wrapper to mem-manager, that looks at the flag and does the counting. But even that will count stuff like  "result := copy(str, 1,10);" despite that memory not remaining in the object. Maybe counting the mem like that is the wrong approach. What do you try to learn from that data?