zoukankan      html  css  js  c++  java
  • 8、显示程序占用内存多少.txt

    方法一: 要加单元 PsAPI
    procedure TForm1.tmr1Timer(Sender: TObject);
    begin
     edt1.Text:= format('memory use: %d KB', [GetProcessMemUse(GetCurrentProcessId)]);//方法一
    end;
    
    function TForm1.GetProcessMemUse(PID: Cardinal): Cardinal;    //方法一
    var
    pmc: PPROCESS_MEMORY_COUNTERS; //uses psApi
    ProcHandle: HWND;
    iSize: DWORD;
    begin
      Result := 0;
      iSize := SizeOf(_PROCESS_MEMORY_COUNTERS);
      GetMem(pmc, iSize);
      try
        pmc^.cb := iSize;
        ProcHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
        False, PID); //由PID取得进程对象的句柄
        if GetProcessMemoryInfo(ProcHandle, pmc, iSize) then
        Result := pmc^.WorkingSetSize;
        Result :=Result div 1024;
      finally
        FreeMem(pmc);
      end;
    end;
    
    //*********************************************
    
    方法二:要加单元 PsAPI, Tlhelp32;
    procedure TForm1.tmr1Timer(Sender: TObject);
    var
     str,str2:string;
    begin
     if (GetProcessMemorySize('proj1.exe',str,str2)) then    //方法二 参数要写程序名,不好
      begin
        edt2.Text :='当前内存使用:'+ str +'KB';
      end; 
    end;
    function TForm1.GetProcessMemorySize(ProcessName: string; var Size,    //方法二
      PN: string): Boolean;
    var
    pProcess:THandle;
    MemSize,t:Integer;
    hProcSnap:THandle;
    PPMCSize:Cardinal;
    pe32:TProcessEntry32;
    PPMC:PPROCESS_MEMORY_COUNTERS;
    begin
    
      PPMCSize := SizeOf(PROCESS_MEMORY_COUNTERS);
      GetMem(PPMC, PPMCSize);
      PPMC^.cb:= PPMCSize;
    
      hProcSnap:=CreateToolHelp32SnapShot(TH32CS_SNAPALL,  0);
      if  hProcSnap=INVALID_HANDLE_VALUE  then  Exit;
      pe32.dwSize:=SizeOf(ProcessEntry32);
      if  Process32First(hProcSnap,pe32)=True then
          while  Process32Next(hProcSnap,pe32)=True  do
          begin
              if uppercase(pe32.szExeFile)=uppercase(ProcessName) then
              begin
                pProcess:=OpenProcess(PROCESS_ALL_ACCESS,FALSE, pe32.th32ProcessID);
    
                if (GetProcessMemoryInfo(pProcess,PPMC,PPMCSize)) then
                 begin
                   MemSize:=PPMC^.WorkingSetSize div 1024;
                   t:= Length(IntToStr(MemSize))-2;
                   Size:=IntToStr(MemSize);
                   Insert(',',Size,t);
                   PN:=ProcessName;
                   FreeMem(PPMC);
                   Result:=True;
                 end
                else
                 begin
                   FreeMem(PPMC);
                   Result:=False;
                 end;
    
              end;
          end;
      CloseHandle(pProcess);
      CloseHandle(hProcSnap);
    end;
    

      

  • 相关阅读:
    Java Output流写入包装问题
    SpringBoot项目单元测试不经过过滤器问题
    SpringSecurity集成启动报 In the composition of all global method configuration, no annotation support was actually activated 异常
    JWT jti和kid属性的说明
    Maven 排除依赖
    第五章 基因概念的发现
    第三章 孟德尔遗传的拓展
    第二章 孟德尔遗传
    第一章 引言
    GWAS全基因组关联分析
  • 原文地址:https://www.cnblogs.com/rogge7/p/4422148.html
Copyright © 2011-2022 走看看