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;
    

      

  • 相关阅读:
    我是新手,我在学Android
    WAMP环境下(apache2.2.21+Php5.3.10)编写PHP扩展
    java中三种字符串正则匹配方式
    PHP扩展开发过程中的问题
    Android错误解决方法之:Debug certificate expired on
    Ubuntu下C++开发PHP开发扩展的注意事项
    我的笔试题
    很多时候,是否好好看完一本好书,对一个人的提升往往能达到质的区别
    C语言常用宏定义
    Linux下用C++开发PHP扩展
  • 原文地址:https://www.cnblogs.com/rogge7/p/4422148.html
Copyright © 2011-2022 走看看