delphi 获取系统内存状态  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.039093971252441 seconds