Windows API一日一練(91)GetProcessMemoryInfo函數

來源:互聯網
上載者:User
  Windows API一日一練(91)GetProcessMemoryInfo函數

當大家開啟Windows工作管理員時,就會看到每個進程使用記憶體的分布情況,往往會發現有一些進程佔用大量的記憶體,在這種情況也是一種異常情況,可以作為是否惡意軟體的標誌之一。下面就來使用API函數GetProcessMemoryInfo來擷取記憶體的使用方式。

函數GetProcessMemoryInfo聲明如下:

BOOL
WINAPI
GetProcessMemoryInfo(
    HANDLE Process,
    PPROCESS_MEMORY_COUNTERS ppsmemCounters,
    DWORD cb
    );
Process是擷取記憶體使用量情況的進程控制代碼。
ppsmemCounters是返回記憶體使用量情況的結構。
cb是結構的大小。

調用函數的例子如下:
#001 //擷取某一個進程的記憶體資訊。 
#002  //蔡軍生 2007/12/18 QQ:9073204 深圳
#003  void TestGetProcessMemoryInfo(void)
#004  {
#005        //
#006        const int nBufSize = 512;
#007        TCHAR chBuf[nBufSize];
#008        ZeroMemory(chBuf,nBufSize);
#009
#010        //
#011        DWORD dwProcs[1024];
#012        DWORD dwNeeded;
#013
#014        //枚舉所有進程ID。
#015        if ( !EnumProcesses( dwProcs, sizeof(dwProcs), &dwNeeded ) )
#016        {
#017              //輸出出錯資訊。
#018              wsprintf(chBuf,_T("EnumProcesses failed (%d)./n"), GetLastError() );
#019              OutputDebugString(chBuf);
#020
#021              return;
#022        }   
#023
#024        // 計算有多少個進程ID。
#025        DWORD dwProcCount = dwNeeded / sizeof(DWORD);
#026
#027        wsprintf(chBuf,_T("EnumProcesses Count(%d)./n"), dwProcCount );
#028        OutputDebugString(chBuf);
#029
#030        //遍曆所有進程ID,開啟進程。
#031        for (DWORD i = 0; i < dwProcCount; i++)
#032        {
#033              wsprintf(chBuf,_T("EnumProcesses (%d)./r/n"), dwProcs );
#034              OutputDebugString(chBuf);
#035
#036              //根據進程ID開啟進程。
#037              HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
#038                    PROCESS_VM_READ,
#039                    FALSE, dwProcs );               
#040
#041              if (hProcess)
#042              {
#043                    //
#044                    PROCESS_MEMORY_COUNTERS pmc;
#045                    pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
#046                   
#047                    //擷取這個進程的記憶體使用量情況。
#048                  if ( ::GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
#049                    {
#050                          ZeroMemory(chBuf,nBufSize);
#051
#052                          wsprintf(chBuf,_T("/t缺頁中斷次數: 0x%08X/n"), pmc.PageFaultCount );
#053                          OutputDebugString(chBuf);
#054
#055                          wsprintf(chBuf,_T("/t使用記憶體高峰: 0x%08X/n"),
#056                              pmc.PeakWorkingSetSize );
#057                          OutputDebugString(chBuf);
#058
#059                          wsprintf(chBuf,_T("/t當前使用的記憶體: 0x%08X/n"), pmc.WorkingSetSize );
#060                          OutputDebugString(chBuf);
#061
#062                          wsprintf(chBuf,_T("/t使用頁面緩衝池高峰: 0x%08X/n"),
#063                              pmc.QuotaPeakPagedPoolUsage );
#064                          OutputDebugString(chBuf);
#065
#066                          wsprintf(chBuf,_T("/t使用頁面緩衝池: 0x%08X/n"),
#067                              pmc.QuotaPagedPoolUsage );
#068                          OutputDebugString(chBuf);
#069
#070                          wsprintf(chBuf,_T("/t使用非分頁緩衝池高峰: 0x%08X/n"),
#071                              pmc.QuotaPeakNonPagedPoolUsage );
#072                          OutputDebugString(chBuf);
#073
#074                          wsprintf(chBuf,_T("/t使用非分頁緩衝池: 0x%08X/n"),
#075                              pmc.QuotaNonPagedPoolUsage );
#076                          OutputDebugString(chBuf);
#077
#078                          wsprintf(chBuf,_T("/t使用分頁檔案: 0x%08X/n"), pmc.PagefileUsage );
#079                          OutputDebugString(chBuf);
#080
#081                          wsprintf(chBuf,_T("/t使用分頁檔案的高峰: 0x%08X/n"),
#082                              pmc.PeakPagefileUsage );
#083                          OutputDebugString(chBuf);
#084                    }             
#085
#086                    //
#087                    CloseHandle(hProcess);
#088              }
#089        }       
#090
#091  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.