參考url:
https://blog.csdn.net/fw0124/article/details/6367360/
https://processhacker.sourceforge.io/downloads.php
https://technet.microsoft.com/zh-cn/ms684903(v=vs.90)
https://docs.microsoft.com/en-us/windows/desktop/api/MemoryApi/nf-memoryapi-queryvirtualmemoryinformation
0 相關名詞解釋
PrivateWorkingSet: 進程獨自占用的物理內存
SharedWorkingSet: 進程在物理內存中跟其他進程共享的內存, 例如: dll占用的內存
WorkingSet: 進程占用的物理內存總和, 即PrivateWorkingSet與SharedWorkingSet的和
commitBytes(Private Bytes): 當前進程獨自占用的虛擬內存總和, 包括PrivateWorkingSet 以及換到頁面文件中的部分
根據網上的參考資料, 一個進程總的內存占用包括PrivateWorkingSet, SharedWorkingSet, 在硬盤頁面文件中的部分
1 獲取進程的內存占用的相關方法
<1> GetProcessMemoryInfo
第二個參數返回結果, 不能獲取PrivateWorkingSet
PROCESS_MEMORY_COUNTERS_EX結構中,
WorkingSetSize: 對應任務管理器中的工作集(WorkingSet)
PeakWorkingSetSize: 對應任務管理器中的峰值工作集
PagefileUsage: 對應任務管理器中的提交大小(commitBytes), Windows 7 and Windows Server 2008 R2 and earlier: PagefileUsage is always zero.
PeakPagefileUsage: 峰值提交大小
PrivateUsage: 對應任務管理器中的提交大小(commitBytes)
<2>QueryWorkingSet
只能在app跟system都是x64或者x86的情況下使用, wow64下能返回結果, 但是跟實際完全不一樣
PSAPI_WORKING_SET_BLOCK結構中,
統計Shared ==0(ProcessExplorer中使用該方法)的頁面個數然后 乘以 每個頁面的大小0x1000 就是該進程的PrivateWorkingSet
<3>QueryWorkingSetEx
這個沒用過,因為不知道這個怎么用
<4>NtQuerySystemInformation
這個可以在win7及以上所有環境下使用, 由於是微軟未文檔化的函數,所以不排除將來有修改的可能, 以下例子是從ProcessHacker的源碼中抽取出來的
https://files.cnblogs.com/files/talenth/GetProcessInfo.7z
<5>可能還有其他的方法, 目前沒有找到...