Windows API一日一練(79)GlobalMemoryStatusEx函數

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

在開發軟體的過程中,經常會碰到不同使用者的PC系統配置不一樣。比如有些使用者的系統記憶體配置比較差,這樣處理大量資料時,就不能把大量的資料讀取到記憶體裡處理了。而又有一些使用者的記憶體比較多,或者是機器比較新,那麼就可以載入大量的資料到記憶體裡處理,這樣可以隨著系統的更新,軟體的處理能力能大幅地提高效能。這樣就需要瞭解系統的配置資訊了,最重要的資源之一記憶體,就是最需要瞭解的,需要調用函數GlobalMemoryStatusEx來瞭解記憶體的分配情況。

函數GlobalMemoryStatusEx聲明如下:

WINBASEAPI
BOOL
WINAPI
GlobalMemoryStatusEx(
    __out LPMEMORYSTATUSEX lpBuffer
    );

lpBuffer是接收記憶體資訊的結構。

調用函數的例子如下:
#001  //
#002  //當前系統記憶體資訊。
#003  //蔡軍生 2007/11/27 QQ:9073204 深圳
#004  void MemInfo(void)
#005  {
#006        //擷取記憶體資訊。
#007        MEMORYSTATUSEX memStatex;
#008          memStatex.dwLength = sizeof(memStatex);
#009
#010        if (GlobalMemoryStatusEx(&memStatex))
#011        {
#012              //
#013              const int nBufSize = 512;
#014              TCHAR chBuf[nBufSize];
#015              ZeroMemory(chBuf,nBufSize);
#016
#017              //記憶體使用量率。
#018              wsprintf(chBuf,_T("記憶體使用量率: %u%%/n"),memStatex.dwMemoryLoad);
#019              OutputDebugString(chBuf);
#020
#021              //總共實體記憶體。
#022              wsprintf(chBuf,_T("總共實體記憶體: %u/n"),memStatex.ullTotalPhys );
#023              OutputDebugString(chBuf);
#024
#025              //可用實體記憶體。
#026              wsprintf(chBuf,_T("可用實體記憶體: %u/n"),memStatex.ullAvailPhys );
#027              OutputDebugString(chBuf);
#028
#029              //全部記憶體。
#030              wsprintf(chBuf,_T("全部記憶體: %u/n"),memStatex.ullTotalPageFile );
#031              OutputDebugString(chBuf);
#032
#033              //全部可用的記憶體。
#034              wsprintf(chBuf,_T("全部可用的記憶體: %u/n"),memStatex.ullAvailPageFile);
#035              OutputDebugString(chBuf);
#036
#037              //全部的虛擬記憶體。
#038              wsprintf(chBuf,_T("全部的記憶體: %u/n"),memStatex.ullTotalVirtual);
#039              OutputDebugString(chBuf);
#040
#041        }
#042  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.