最近寫了看門狗的winform程序,需求要求顯示監測的進程所占的內存大小
找了好幾個方法,都和任務管理器顯示的內存不一致
最后找到了一個解決方法
PerformanceCounter pf1 = new PerformanceCounter("Process", "Working Set - Private", process.ProcessName);//第二個參數就是得到只有工作集
string text = $"該進程所占內存為:{ pf1.NextValue() / 1024}KB";
不過用PerformanceCounter的話會導致程序的CPU占用過高,具體可見下面的參考文章
參考文章:https://q.cnblogs.com/q/74255/
http://www.cnblogs.com/maowang1991/archive/2013/08/27/3285983.html