Recent

Author Topic: How to determine maximum memory used by a program?  (Read 3483 times)

Robert.Thompson

  • Jr. Member
  • **
  • Posts: 56
  • "A very bad coder."
    • Google Voice for Canadians
How to determine maximum memory used by a program?
« on: December 31, 2017, 07:24:04 pm »
Hello:

Is there a way to determine the maximum amount of memory that my program used during its' execution?

Thanks,

Rob.
Lazarus:  1.8.4  2018-11-17
FPC:   3.0.4 x86_64-linux-gtk2
System:   Kernel: 4.15.0-39-generic x86_64 bits: 64 gcc: 7.3.0 Cinnamon 3.8.9 Linux Mint 19 Tara
              Phoenix v: 11JB.M044.20100622.hkk date: 06/22/2010
Intel Core i5 M 460 (-MT-MCP-) arch: Nehalem rev.5 cache: 3072 KB
NVIDIA GeForce 310M

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: How to determine maximum memory used by a program?
« Reply #1 on: December 31, 2017, 07:39:12 pm »
You can get an idea using the values returned by this call:

https://www.freepascal.org/docs-html/rtl/system/getfpcheapstatus.html

The interpretation is a bit difficult though.

Robert.Thompson

  • Jr. Member
  • **
  • Posts: 56
  • "A very bad coder."
    • Google Voice for Canadians
Re: How to determine maximum memory used by a program?
« Reply #2 on: January 01, 2018, 03:48:32 pm »
Thank you. :)
Lazarus:  1.8.4  2018-11-17
FPC:   3.0.4 x86_64-linux-gtk2
System:   Kernel: 4.15.0-39-generic x86_64 bits: 64 gcc: 7.3.0 Cinnamon 3.8.9 Linux Mint 19 Tara
              Phoenix v: 11JB.M044.20100622.hkk date: 06/22/2010
Intel Core i5 M 460 (-MT-MCP-) arch: Nehalem rev.5 cache: 3072 KB
NVIDIA GeForce 310M

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: How to determine maximum memory used by a program?
« Reply #3 on: May 16, 2019, 05:09:00 pm »
For information, if the cmem unit is somewhere in "uses" ( http://wiki.lazarus.freepascal.org/CMem ), then GetFPCHeapStatus returns 0 for all memory quantities (normal behaviour).
« Last Edit: May 16, 2019, 05:12:34 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: How to determine maximum memory used by a program?
« Reply #4 on: June 21, 2023, 01:32:14 pm »
ok, if it doesn't work with cmem, how can you find out how much memory is being used on the heap then?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: How to determine maximum memory used by a program?
« Reply #5 on: June 21, 2023, 01:45:31 pm »
If you use an external memory manager like cmem, you will need to make calls to that external memory manager for such stats. ((g)libc or msvcrt).

That is of course assuming that the memory manager has them at all.

Alternately you try to use operating system calls to get info about e.g. data segment size and the like.

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: How to determine maximum memory used by a program?
« Reply #6 on: June 21, 2023, 02:18:40 pm »
it's, umm, surprising that there's no facility in place like that for cmem. And there's no cross=platform way to find out how much the system has allocated to the program?


KodeZwerg

  • Hero Member
  • *****
  • Posts: 2054
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to determine maximum memory used by a program?
« Reply #7 on: June 21, 2023, 02:29:41 pm »
Maybe this helps on your journey about memory?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   Heaptrc
  5.   {$IFDEF WINDOWS}
  6.   , Windows
  7.   {$ENDIF}
  8.   ;
  9.  
  10. const
  11.   CTestSize = DWORD(1024);
  12.  
  13. {$IFDEF WINDOWS}
  14. type
  15.   PPROCESS_MEMORY_COUNTERS = ^TProcessMemoryCounters;
  16.   TProcessMemoryCounters = record
  17.     cb: DWORD;
  18.     PageFaultCount: DWORD;
  19.     PeakWorkingSetSize: SIZE_T;
  20.     WorkingSetSize: SIZE_T;
  21.     QuotaPeakPagedPoolUsage: SIZE_T;
  22.     QuotaPagedPoolUsage: SIZE_T;
  23.     QuotaPeakNonPagedPoolUsage: SIZE_T;
  24.     QuotaNonPagedPoolUsage: SIZE_T;
  25.     PagefileUsage: SIZE_T;
  26.     PeakPagefileUsage: SIZE_T;
  27.   end;
  28.  
  29. function GetProcessMemoryInfo(hProcess: THandle; ppsmemCounters: PPROCESS_MEMORY_COUNTERS;
  30.   cb: DWORD): BOOL; stdcall; external 'PsApi.dll' name 'GetProcessMemoryInfo';
  31. {$ENDIF}
  32.  
  33. var
  34.   LP: Pointer;
  35.   LHeapStatus: THeapStatus;
  36.   {$IFDEF WINDOWS}
  37.   LProcessHandle: THandle;
  38.   LProcessMemoryCounters: TProcessMemoryCounters;
  39.   {$ENDIF}
  40. begin
  41.   // Allocate some memory to use
  42.   GetMem(LP, CTestSize);
  43.  
  44.   // Get the current heap status
  45.   LHeapStatus := GetHeapStatus;
  46.  
  47.   // Display the amount of used memory
  48.   WriteLn('Used memory (Heap): ', LHeapStatus.TotalAddrSpace - LHeapStatus.TotalUncommitted);
  49.  
  50.   // Windows method
  51.   {$IFDEF WINDOWS}
  52.   // Get the process handle for the current process
  53.   LProcessHandle := GetCurrentProcess;
  54.  
  55.   // Get the memory usage information for the process
  56.   LProcessMemoryCounters.cb := SizeOf(LProcessMemoryCounters);
  57.   if GetProcessMemoryInfo(LProcessHandle, @LProcessMemoryCounters, SizeOf(LProcessMemoryCounters)) then
  58.   begin
  59.     // Display the amount of used memory
  60.     WriteLn('Used memory (Windows): ', LProcessMemoryCounters.WorkingSetSize);
  61.   end;
  62.   ReadLn;
  63.   {$ENDIF}
  64.  
  65.   // Free the memory
  66.   FreeMem(LP, CTestSize);
  67. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: How to determine maximum memory used by a program?
« Reply #8 on: June 21, 2023, 03:13:31 pm »
it's, umm, surprising that there's no facility in place like that for cmem.

C never standardized that, so all various cmem backends differ.

Quote
And there's no cross=platform way to find out how much the system has allocated to the program?

No. Numbers often need interpretation(do shared or code area's count?  Really allocated vs allocated address space etc), and there is no commonality in the APIs. 

So it often depends on what you need it for, and how precise it needs to be.




Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: How to determine maximum memory used by a program?
« Reply #9 on: June 21, 2023, 08:15:19 pm »
Wow. This is way harder than I expected it to be. What I want is something that seems super obvious to me: a number for how much memory is in use so I can log it. @KodeZwerg thanks for the windows code. For linux, it seems as though I read /proc/meminfo.

But that doesn't work for OSX. It seems like task_info is the thing I want, but I can't find a FPC translation for task_info.h. Is that because I'm looking in the wrong place or in the wrong way, or is it something that should be done? I'd do it, but I know nothing about header file translation on OSX

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 402
Re: How to determine maximum memory used by a program?
« Reply #10 on: June 21, 2023, 08:32:35 pm »
Out of curiosity, why do you need to know (and how accurately)?   Would a Linux function like pmap be of use (if available in OSX?)

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: How to determine maximum memory used by a program?
« Reply #11 on: June 21, 2023, 08:54:50 pm »
I have a web server that runs on windows and monitor its memory usage, and cleans out caches when it starts running out of memory. it works on windows, but I'm trying to get off windows for stability purposes.

I can't get it to work on linux (intended server) or OSX (my dev), so that's what I'm trying to sort out.

pmap is cli application. How would that help me?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: How to determine maximum memory used by a program?
« Reply #12 on: June 21, 2023, 09:10:15 pm »
Note that your process size and running out of memory are two different things.

I don't know anything about OS X apis.

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: How to determine maximum memory used by a program?
« Reply #13 on: June 21, 2023, 09:12:53 pm »
I'm aware that they're different things, but they're somewhat interchangeable - something I can manage in config. But I can't get either.... I'm blind.

For now, I have removed cmem so I can get the internal heap size. It's not clear to me where I got the idea that cmem is a requirement on unix, but I did somewhere...

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2054
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to determine maximum memory used by a program?
« Reply #14 on: June 22, 2023, 02:28:56 am »
By reading I would suggest to not focus your app(s) or thread(s) memory usage, just have a look on available and free memory.
Sorry that I can again just show the Windows implementation that I am aware of:
Code: Pascal  [Select][+][-]
  1. type
  2.   _MEMORYSTATUSEX = record
  3.     dwLength: DWORD;
  4.     dwMemoryLoad: DWORD;
  5.     ullTotalPhys: UInt64;
  6.     ullAvailPhys: UInt64;
  7.     ullTotalPageFile: UInt64;
  8.     ullAvailPageFile: UInt64;
  9.     ullTotalVirtual: UInt64;
  10.     ullAvailVirtual: UInt64;
  11.     ullAvailExtendedVirtual: UInt64;
  12.   end;
  13.   TMemoryStatusEx = _MEMORYSTATUSEX;
  14.  
  15. function GlobalMemoryStatusEx(var lpBuffer: TMemoryStatusEx): BOOL; stdcall; external kernel32;
  16.  
  17. var
  18.   MemoryStatus: TMemoryStatusEx;
  19. begin
  20.   // Initialize the MEMORYSTATUSEX structure
  21.   MemoryStatus.dwLength := SizeOf(MemoryStatus);
  22.  
  23.   // Get the memory status information
  24.   if not GlobalMemoryStatusEx(MemoryStatus) then
  25.     begin
  26.       WriteLn('Error: ', SysErrorMessage(GetLastError));
  27.       ReadLn;
  28.       Exit;
  29.     end;
  30.  
  31.   WriteLn('Total physical memory: ', MemoryStatus.ullTotalPhys div 1024, ' KB');
  32.   WriteLn('Available physical memory: ', MemoryStatus.ullAvailPhys div 1024, ' KB');
  33.   ReadLn;
  34. end.
Anyway, that is just the physical memory, virtual memory is a total different story but same easy to measure.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018