GetFPCHeapStatus is thread specific lazarus / 21-General ---------------------------------------------------------------------- Grahame Grieve · 2023-06-21 20:32 I'm reporting the output from GetFPCHeapStatus GetFPCHeapStatus and GetHeapStatus in my log. Weirdly, the output from both of them seems to be thread specific. or maybe it's just unreliable... Is this a known thing? Lazarus 2.2.7 (rev lazarus_2_2_6-1-gada7a90f86) FPC 3.2.3 aarch64-darwin-cocoa ---------------------------------------------------------------------- Grahame Grieve · 2023-06-21 21:55 Here's a demo: 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  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes, SysUtils, CustApp  ;  function memSize : UInt64;var  hs : TFPCHeapStatus;begin  hs := GetFPCHeapStatus;  result := hs.CurrHeapSize;end;  type  TMyThread = class (TThread)  protected    procedure Execute; override;  end; procedure TMyThread.Execute;begin   writeln('t = '+inttoStr(memSize));end; procedure showMemoryUsage;var  i : integer;begin  getmem(1000);  writeln('m = '+inttoStr(memSize));  for i := 0 to 10000 do    getmem(10000);  writeln('m = '+inttoStr(memSize));  writeln('Starting thread. Press Enter to close');  TMyThread.create(false);  readln;end; type  { TMyApplication }   TMyApplication = class(TCustomApplication)  protected    procedure DoRun; override;  public    constructor Create(TheOwner: TComponent); override;    destructor Destroy; override;    procedure WriteHelp; virtual;  end; { TMyApplication } procedure TMyApplication.DoRun;var  ErrorMsg: String;begin  // quick check parameters  ErrorMsg:=CheckOptions('h', 'help');  if ErrorMsg<>'' then begin    ShowException(Exception.Create(ErrorMsg));    Terminate;    Exit;  end;   // parse parameters  if HasOption('h', 'help') then begin    WriteHelp;    Terminate;    Exit;  end;   { add your program here }  ShowMemoryUsage;   // stop program loop  Terminate;end; constructor TMyApplication.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);  StopOnException:=True;end; destructor TMyApplication.Destroy;begin  inherited Destroy;end; procedure TMyApplication.WriteHelp;begin  { add your help code here }  writeln('Usage: ', ExeName, ' -h');end; var  Application: TMyApplication;begin  Application:=TMyApplication.Create(nil);  Application.Title:='My Application';  Application.Run;  Application.Free;end.  Output: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i m = 753664m = 101416960Starting thread. Press Enter to closet = 0 ---------------------------------------------------------------------- Grahame Grieve · 2023-06-23 02:17 Apparently this is known but it's sure under-documented, and so far as I can figure out, there's no way to get total memory allocated for a process ---------------------------------------------------------------------- 440bx · 2023-06-23 03:14 Quote from: Grahame Grieve on June 23, 2023, 04:17:18 amthere's no way to get total memory allocated for a process One of the reasons is that there is usually a fair amount of memory that is shared among processes, should that shared memory be included or excluded as memory allocated for a particular process ? Also, memory (a page) may be allocated but not present.  Should those pages be counted when they are not present ? (they aren't using physical memory) In addition to those, there are also ring-0 structures that are per process.  Those should probably be counted but since they reside in ring-0, there is no way for a program or a user to account for that memory (and for security reasons, the O/S should probably abstain from revealing what the memory is used for.) Answering the question: "how much memory is a process consuming ?" isn't quite as simple as it would first seem. On Windows, using documented and undocumented functions (in ntdll), it is possible to create a reasonably accurate memory map which in turn enables a reasonable answer to that question.   I don't know what Linux/Unix offers in that area but, I am under the impression its offerings (APIs) are fewer and more limited. ---------------------------------------------------------------------- Grahame Grieve · 2023-06-23 06:15 Right, but in every other platform I've developed on, there's at least a way to find out how much memory you've allocated. Delphi's heap status reports the heap for all threads. Given that the heap is shared between threads, and memory passed between them, I cannot make sense of having getFPCHeapStatus reporting on a per thread basis. How can that possibly make sense? ---------------------------------------------------------------------- 440bx · 2023-06-23 06:57 Quote from: Grahame Grieve on June 23, 2023, 08:15:50 amI cannot make sense of having getFPCHeapStatus reporting on a per thread basis. How can that possibly make sense? What follows is what I vaguely remember from a discussion I had about FPC's heaps a few years back. In that discussion I was left with the impression that FPC tracks heap memory allocations per thread.  If that is correct then, it would make sense that it would report heap status/allocations per thread (since that is how it allocates them.) Of course, that method makes it a bit complicated to determine the total amount of heap memory used. ---------------------------------------------------------------------- runewalsh · 2023-06-23 08:03 Global heap status would be close to useless anyway. You got the information you have 10 Mb used, and a second later, another thread allocates 100 Mb (or vice versa: you have 110 Mb used, then another thread frees 100 it worked on), how are you going to use this knowledge... If you really need to, you can set up your manager with GetMemoryManager + SetMemoryManager that maintains global counters and delegates the rest of the work to the previous manager; related bugs were fixed long ago. ---------------------------------------------------------------------- BrunoK · 2023-06-23 17:48 Quote from: 440bx on June 23, 2023, 08:57:43 amIn that discussion I was left with the impression that FPC tracks heap memory allocations per thread.  If that is correct then, it would make sense that it would report heap status/allocations per thread (since that is how it allocates them.) Yes I think it works like that. What has not been clarified is how the real memory allocated is freed when the thread having allocated system memory blocks terminates. I think the memory is marked as freed but not reusable by other threads. A very discreet effective resource loss. Cmem could be better in this respect, but is butchered in FPC. A rare point where I did agree with Thaddy (alias Grumpy). ---------------------------------------------------------------------- runewalsh · 2023-06-23 18:43 Quote from: BrunoK on June 23, 2023, 07:48:02 pmYes I think it works like that. What has not been clarified is how the real memory allocated is freed when the thread having allocated system memory blocks terminates. heap.inc assumes that deallocation in the same thread is the common case, so it checks if the chunk is being deallocated by the different thread, and if so, instead adds it to the to-do list of the correct thread, analyzed later by that thread when it runs out of free memory. (I suspect scenarios that suffer from that are possible: allocate 1 Gb in thread A, deallocate in thread B, now this 1 Gb hangs around until you do enough allocations in thread A or until it terminates, which might not happen soon...). ---------------------------------------------------------------------- 440bx · 2023-06-23 21:04 Quote from: runewalsh on June 23, 2023, 08:43:24 pm ... checks if the chunk is being deallocated by the different thread, and if so, instead adds it to the to-do list of the correct thread, Personally, I'd rather have the deallocation by a different thread considered an error. ---------------------------------------------------------------------- Grahame Grieve · 2023-06-23 21:12 no, deallocation by another thread is very much not an error, it's a very common thing in a server. Else how can you pass tasks between threads? ---------------------------------------------------------------------- Grahame Grieve · 2023-06-23 21:13 > If you really need to, you can set up your manager with GetMemoryManager + SetMemoryManager Working on that now, and a discovered issue: Freemem takes (nil) as a value, and that is called in places, but memsize blows up if you pass it nil ---------------------------------------------------------------------- 440bx · 2023-06-23 22:21 Quote from: Grahame Grieve on June 23, 2023, 11:12:27 pmno, deallocation by another thread is very much not an error, it's a very common thing in a server. Else how can you pass tasks between threads? It breaks parallelism.  Whenever I see that happen in my code, I consider it erroneous and review it until I find a way to restore parallelism. ---------------------------------------------------------------------- Grahame Grieve · 2023-06-23 22:29 Well, good luck. but I do not, and could not use FPC if the memory manager made such a rule ---------------------------------------------------------------------- Grahame Grieve · 2023-06-23 22:42 Anyway, in case it's interesting for other people, here's my implementation 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 unit fsl_fpc_memory; {Copyright (c) 2011+, Health Intersections Pty Ltd (http://www.healthintersections.com.au)All rights reserved. Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met:  * Redistributions of source code must retain the above copyright notice, this   list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice,   this list of conditions and the following disclaimer in the documentation   and/or other materials provided with the distribution. * Neither the name of HL7 nor the names of its contributors may be used to   endorse or promote products derived from this software without specific   prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUTNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.} {$I fhir.inc} interface type   { TFPCMemoryManagerTracker }   TFPCMemoryManagerTracker = class  public    class function totalMemory : UInt64;    class procedure install;  end; implementation { TFPCMemoryManagerTracker } var  GRealMM : TMemoryManager;  GFacadeMM : TMemoryManager;  GTotalMemory : UInt64; Function RGetMem(Size:ptruint):Pointer;begin  result := GRealMM.GetMem(size);  if (result <> nil) then    interlockedExchangeAdd64(GTotalMemory, GRealMM.MemSize(result));end; Function RFreemem(p:pointer):ptruint;begin  if (p <> nil) then    interlockedExchangeAdd64(GTotalMemory, -GRealMM.MemSize(p));  result := GRealMM.FreeMem(p);end; Function RFreememSize(p:pointer;Size:ptruint):ptruint;begin  if (p <> nil) then    interlockedExchangeAdd64(GTotalMemory, -GRealMM.MemSize(p));  result := GRealMM.FreememSize(p, size);end; Function RAllocMem(Size:ptruint):Pointer;begin  result := GRealMM.AllocMem(Size);  if (result <> nil) then    interlockedExchangeAdd64(GTotalMemory, GRealMM.MemSize(result));end; Function RReAllocMem(var p:pointer;Size:ptruint):Pointer;var  s : UInt64;begin  if (p <> nil) then    s := GRealMM.MemSize(p)  else    s := 0;  result := GRealMM.ReAllocMem(p, size);  if (result <> nil) then    interlockedExchangeAdd64(GTotalMemory, GRealMM.MemSize(result)-s)  else    interlockedExchangeAdd64(GTotalMemory, -s)end; class function TFPCMemoryManagerTracker.totalMemory: UInt64;begin  result := GTotalMemory;end; class procedure TFPCMemoryManagerTracker.install;begin  GetMemoryManager(GRealMM);  GFacadeMM.NeedLock            := GRealMM.NeedLock;  GFacadeMM.Getmem            := RGetMem;  GFacadeMM.Freemem             := RFreemem;  GFacadeMM.FreememSize         := RFreememSize;  GFacadeMM.AllocMem          := RAllocMem;  GFacadeMM.ReAllocMem          := RReAllocMem;  GFacadeMM.MemSize             := GRealMM.MemSize;  GFacadeMM.InitThread          := GRealMM.InitThread;  GFacadeMM.DoneThread          := GRealMM.DoneThread;  GFacadeMM.RelocateHeap        := GRealMM.RelocateHeap;  GFacadeMM.GetHeapStatus       := GRealMM.GetHeapStatus;  GFacadeMM.GetFPCHeapStatus    := GRealMM.GetFPCHeapStatus;  GTotalMemory := GetFPCHeapStatus.CurrHeapSize;  SetMemoryManager(GFacadeMM);end; end.  The only interesting thing the include does for this unit is turn Delphi mode on ---------------------------------------------------------------------- Grahame Grieve · 2023-06-23 22:44 This slowed my start up by 0.4%, btw (on my Mac m1) ---------------------------------------------------------------------- runewalsh · 2023-06-24 12:12 Do GFacadeMM := GRealMM and set only what you need. C: And you don’t need global GFacadeMM, SetMemoryManager copies it. ---------------------------------------------------------------------- Grahame Grieve · 2023-06-25 19:43 Thx. Current code here: https://github.com/HealthIntersections/fhirser ver/blob/master/library/fsl/fsl_fpc_memory.pas