簡體   English   中英

Process.PrivateMemorySize64返回已提交的內存,而不是私有的

[英]Process.PrivateMemorySize64 returning committed memory instead of private

我正在使用.NET 4.5用C#編寫一個程序,該程序將允許我監視特定進程的內存,CPU和網絡使用情況,然后根據需要繪制該數據圖表。

為了獲得特定進程的內存使用情況,我正在檢查該Process對象的PrivateMemorySize64屬性。 我期望看到該進程使用的專用內存,但相反,它顯示了“提交”中的數量,這已由Windows資源監視器確認。

我的問題是:

1)有人知道為什么會發生此錯誤嗎? 2)是否有解決方法? 3)如果沒有修復,是否還有另一種直接的方法可以獲取為進程保留的專用內存?

這是我的代碼的相關部分:

using System;

// I add all the open Processes to an array
Process[] localAll = Process.GetProcesses();

// I then add all the processes to a combobox to select from
// There's a button that updates labels with requested info

Process[] p = Process.GetProcessesByName(comboBox1.SelectedItem.ToString());
label1.Text = p[0].PrivateMemorySize64.ToString() + " bytes";

從您的評論中,您說您正在尋找私有工作集。 從此鏈接中出現如何計算私有工作集(內存)? 它確實不是Process類的一部分。 您必須改為使用性能計數器。

從其他答案中復制並粘貼,以防萬一由於某種原因將其刪除。

using System;
using System.Diagnostics;

class Program {
    static void Main(string[] args) {
        string prcName = Process.GetCurrentProcess().ProcessName;
        var counter = new PerformanceCounter("Process", "Working Set - Private", prcName);
        Console.WriteLine("{0}K", counter.RawValue / 1024);
        Console.ReadLine();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM