Recent

Author Topic: Show memory usage of application  (Read 13275 times)

totya

  • Hero Member
  • *****
  • Posts: 722
Show memory usage of application
« on: February 01, 2011, 10:24:07 am »
Hi!

I want see memory usage of my application in my application. This is possible?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12142
  • FPC developer.
Re: Show memory usage of application
« Reply #1 on: February 01, 2011, 10:39:59 am »
Search in the help/docs for getheapstatus and getfpcheapstatus. IIRC they come with examples.

totya

  • Hero Member
  • *****
  • Posts: 722
Re: Show memory usage of application
« Reply #2 on: February 02, 2011, 03:38:48 pm »
Search in the help/docs for getheapstatus and getfpcheapstatus. IIRC they come with examples.

Thanks, I see. But I don't know what is this :"IIRC".

ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: Show memory usage of application
« Reply #3 on: February 02, 2011, 04:24:25 pm »
Search in the help/docs for getheapstatus and getfpcheapstatus. IIRC they come with examples.

Thanks, I see. But I don't know what is this :"IIRC".

Abbreviation for "If I Recall Correctly"

totya

  • Hero Member
  • *****
  • Posts: 722
Re: Show memory usage of application
« Reply #4 on: February 02, 2011, 04:51:26 pm »
... I don't know what is this :"IIRC".
Abbreviation for "If I Recall Correctly"

Thanks :)

Dibo

  • Hero Member
  • *****
  • Posts: 1055
Re: Show memory usage of application
« Reply #5 on: February 03, 2011, 06:57:17 pm »
Records returned by this funcions has many variables but none reflect memory usage which show system (windows) task manager. Exists any function to get this?

peardox

  • Jr. Member
  • **
  • Posts: 70
Re: Show memory usage of application
« Reply #6 on: February 08, 2011, 01:06:20 pm »
I've been trying to find out how to do this for about a week.

I got bored up of looking for a spoon-fed answer so decided to work it out myself.

A quick play gets me the example below - the number you're after is WorkingSetSize

Windows ONLY!!!

Drop a memo and button onto a form then do this...

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, jwapsapi, jwawinbase, jwawinnt;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  psmemCounters: _PROCESS_MEMORY_COUNTERS;
  ThisApp: LongWord;
begin
  ThisApp := GetCurrentProcessID();
  ThisApp := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, ThisApp );
  if( ThisApp = LongWord(0)) then
    begin
    Application.Messagebox('OpenProcess Error', 'Error', 0);
    exit;
    end;
  if(GetProcessMemoryInfo(ThisApp, psmemCounters, sizeof(_PROCESS_MEMORY_COUNTERS)) <> LongBool(0)) then
    begin
    Memo1.Clear;
    Memo1.lines.add('WorkingSetSize ' + IntToStr(psmemCounters.WorkingSetSize));
    Memo1.lines.add('');
    Memo1.lines.add('PageFileUsage ' + IntToStr(psmemCounters.PagefileUsage));
    Memo1.lines.add('QuotaNonPagedPoolUsage ' + IntToStr(psmemCounters.QuotaNonPagedPoolUsage));
    Memo1.lines.add('QuotaPagedPoolUsage ' + IntToStr(psmemCounters.QuotaPagedPoolUsage));
    Memo1.lines.add('PageFaultCount ' + IntToStr(psmemCounters.PageFaultCount));
    Memo1.lines.add('');
    Memo1.lines.add('PeakWorkingSetSize ' + IntToStr(psmemCounters.PeakWorkingSetSize));
    Memo1.lines.add('PeakPageFileUsage ' + IntToStr(psmemCounters.PeakPagefileUsage));
    Memo1.lines.add('QuotaPeakNonPagedPoolUsage ' + IntToStr(psmemCounters.QuotaPeakNonPagedPoolUsage));
    Memo1.lines.add('QuotaPeakPagedPoolUsage ' + IntToStr(psmemCounters.QuotaPeakPagedPoolUsage));
    end
  else
    begin
    Application.Messagebox('GetProcessMemoryInfo Error', 'Error', 0);
    end;
  CloseHandle(ThisApp);
end;

initialization
  {$I unit1.lrs}

end.

I just threw this together then compared it to Windows Task Manager's value and it was correct (more accurate I guess as it reports to the byte rather than in k)

Useless for cross-platform of course :(
« Last Edit: February 08, 2011, 01:09:55 pm by peardox »

 

TinyPortal © 2005-2018