[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Monitor Memory Usage of my own Process

Posted on 2004-04-30
7
Medium Priority
?
1,688 Views
Last Modified: 2010-05-20
How do I Monitor Memory Usage of my own Process?

I'm interested in things like Virtual Memory (Current & Peak) and Working Set Size (Current & Peak).  All the sort of things that TaskManager shows.  Only just for my own process.
0
Comment
Question by:PHenningsen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10965497

Here are some functions that should give you what you are after:

// Get minimum working set size
function GetMinWorkingSetSize: DWORD;
var  dwTemp:        DWORD;
begin

  if not(GetProcessWorkingSetSize(GetCurrentProcess, result, dwTemp)) then result:=0;

end;

// Get maximum working set size
function GetMaxWorkingSetSize: DWORD;
var  dwTemp:        DWORD;
begin

  if not(GetProcessWorkingSetSize(GetCurrentProcess, dwTemp, result)) then result:=0;

end;

// Get number of bytes allocated for delphi's heap manager
function GetCurrentSetSize: DWORD;
var  lpHeapStatus:  THeapStatus;
begin

  lpHeapStatus:=GetHeapStatus;
  result:=lpHeapStatus.TotalAddrSpace;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  ShowMessage(IntToStr(GetMinWorkingSetSize));
  ShowMessage(IntToStr(GetMaxWorkingSetSize));
  ShowMessage(IntToStr(GetCurrentSetSize));

end;


Regards,
Russell
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10965777
Also, you may want to look into the GetHeapStatus function (used in the GetCurrentSize function). This returns a structure detailing current memory usage for the delphi process.

type

  THeapStatus = record
    TotalAddrSpace: Cardinal;s
    TotalUncommitted: Cardinal;
    TotalCommitted: Cardinal;
    TotalAllocated: Cardinal;
    TotalFree: Cardinal;
    FreeSmall: Cardinal;
    FreeBig: Cardinal;
    Unused: Cardinal;
    Overhead: Cardinal;
    HeapErrorCode: Cardinal;

  end;

Also, there is AllocMemCount/AllocMemSize that can return the number of allocated memory blocks, and the size of the allocated memory blocks (respectively)

Russell
0
 

Author Comment

by:PHenningsen
ID: 10982380
As I understand it, Min & Max Working Set Size are "desired" limits.  I'd like to find Current & Peak Working Set Size.  I believe that it is these two values that Task Manager calls "Mem Usage"

I'll have to experiment with the numbers from THeapStatus.  Delphi's help info did not clarify it.

AllocMemSize does not measure Current Working Set Size
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 26

Expert Comment

by:Russell Libby
ID: 10982476

My misunderstanding on what you were after....

Check GetProcessMemoryInfo which is available from the psapi.pas unit. The GetProcessMemoryInfo function will return the following information in a record structure.

typedef struct _PROCESS_MEMORY_COUNTERS {
    DWORD cb;
    DWORD PageFaultCount;
    SIZE_T PeakWorkingSetSize;
    SIZE_T WorkingSetSize;
    SIZE_T QuotaPeakPagedPoolUsage;
    SIZE_T QuotaPagedPoolUsage;
    SIZE_T QuotaPeakNonPagedPoolUsage;
    SIZE_T QuotaNonPagedPoolUsage;
    SIZE_T PagefileUsage;
    SIZE_T PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;

This will return the peak and current working set size for the process. If you need an example of usage, just ask.

Regards,
Russell


0
 

Author Comment

by:PHenningsen
ID: 10982839
I would love an example.  Thanks
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 2000 total points
ID: 10982952
No problem....
Same display info as what is shown in the Task manager (Mem Usage, peak Mem Usage), except displayed in the form caption

Russell

------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Psapi;

type
  TForm1         = class(TForm)
     procedure   FormCreate(Sender: TObject);
     procedure   FormClose(Sender: TObject; var Action: TCloseAction);
  private
     // Private declarations
     FTimer:     TTimer;
  protected
     // Protected declarations
     procedure   OnTimer(Sender: TObject);
  public
     // Public declarations
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.OnTimer(Sender: TObject);
var  psmemCounters: _PROCESS_MEMORY_COUNTERS;
begin

  psmemCounters.cb:=SizeOf(_PROCESS_MEMORY_COUNTERS);
  if GetProcessMemoryInfo(GetCurrentProcess, @psmemCounters, SizeOf(_PROCESS_MEMORY_COUNTERS)) then
  begin
     // Display in KB
     Caption:=Format('PeakWorkingSetSize = %d KB, WorkingSetSize = %d KB',
                     [psmemCounters.PeakWorkingSetSize div 1024,
                     psmemCounters.WorkingSetSize div 1024]);
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  FTimer:=TTimer.Create(Self);
  FTimer.Interval:=1000;
  FTimer.OnTimer:=Self.OnTimer;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  FTimer.Free;

end;

end.


0
 

Author Comment

by:PHenningsen
ID: 10983101
Thank you so much!

My EE-display has changed like to "inquiry only" and I can't find the "Accept" button.

I'll try harder in a few minutes...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question