Delphi 获取系统内存状态


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Memory Status

type
  DWORDLONG = UInt64;
  TMemoryStatusEx = record
    dwLength: DWORD;
    dwMemoryLoad: DWORD;
    ullTotalPhys: DWORDLONG;
    ullAvailPhys: DWORDLONG;
    ullTotalPageFile: DWORDLONG;
    ullAvailPageFile: DWORDLONG;
    ullTotalVirtual: DWORDLONG;
    ullAvailVirtual: DWORDLONG;
    ullAvailExtendedVirtual: DWORDLONG;
  end;

function GlobalMemoryStatusEx(var lpBuffer: TMEMORYSTATUSEX): BOOL; stdcall; external kernel32;

function MemoryStatus(var InUse: Integer; var PhysicalTotal, PhysicalFree,
  VirtualTotal, VirtualFree, PagingTotal, PagingFree: UInt64): Boolean;
var Status: TMemoryStatusEx;
begin
  Status.dwLength := SizeOf(Status);
  Result := GlobalMemoryStatusEx(Status);
  if Result then
  begin
    InUse := Status.dwMemoryLoad;
    PhysicalTotal := Status.ullTotalPhys;
    PhysicalFree := Status.ullAvailPhys;
    VirtualTotal := Status.ullTotalVirtual;
    VirtualFree := Status.ullAvailVirtual;//delphitop.com
    PagingTotal := Status.ullTotalPageFile;
    PagingFree := Status.ullAvailPageFile;
  end;
end;




procedure TForm1.Button1Click(Sender: TObject);
const
  MB = 1024 * 1024;
var
  Text: string;
  InUse: Integer;
  PhysicalTotal, PhysicalFree: UInt64;
  VirtualTotal, VirtualFree: UInt64;
  PagingTotal, PagingFree: UInt64;
begin
  if MemoryStatus(InUse, PhysicalTotal, PhysicalFree,
    VirtualTotal, VirtualFree, PagingTotal, PagingFree) then
  begin
    Text := 'Memory in use: ' + IntToStr(InUse) + '%' + #13#10 +
      'Total physical memory: ' + IntToStr(PhysicalTotal div MB) + ' MB' + #13#10 +
      'Free physical memory: ' + IntToStr(PhysicalFree div MB) + ' MB' + #13#10  +
      'Total virtual memory: ' + IntToStr(VirtualTotal div MB) + ' MB' + #13#10 +
      'Free virtual memory: ' + IntToStr(VirtualFree div MB) + ' MB' + #13#10  +
      'Total paging file: ' + IntToStr(PagingTotal div MB) + ' MB' + #13#10 +
      'Free paging file: ' + IntToStr(PagingFree div MB) + ' MB';
      Memo1.Clear;
    Memo1.Lines.Add(Text);

  end;
end;

end.

相关阅读 >>

Delphi 锁住listview防止刷新

Delphi中webbrowser问题集锦

Delphi中进行指纹仪的二次开发

Delphi获取宽带ip

Delphi playsound(); 停止播放

Delphi superobject 序列数据集

Delphi 使用管道进程间通讯

tstrings 的用法

Delphi 注册表管理

shellexecute()和winexec()区别

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...