2012-04-17 57 views
1

我試圖使用內存的性能計數器:計數器是單個實例,實例名稱「WebDev.WebServer40」是不適用於此計數器

System.Diagnostics.PerformanceCounter theMemCounter = 
    new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes", 
    System.Diagnostics.Process.GetCurrentProcess().ProcessName, true); 

var memStart = theMemCounter.NextValue(); 

但在第二行,我發現了以下錯誤:

Counter is single instance, instance name 'WebDev.WebServer40' is not valid for this counter category. 

問題是什麼?

+0

如果你打開perfmon的,你看到的內存類別內的櫃檯? – Chris 2012-04-17 12:37:44

+0

您是否在談論屬性CategoryName?如果是的話,那就是:'CategoryName =「Memory」'。 – 2012-04-17 13:05:17

回答

2

Ottoni,我不認爲你可以指定一個進程到這個特定的性能計數器,因爲它監視整個系統的可用內存。您可能正在尋找的性能是「.NET CLR內存(INSTANCE)#所有堆中的字節數」或.NET CLR內存類別中的某些其他內存類別,該類別能夠監控所有或指定的內存使用情況.net應用程序。這一類在這裏

更多信息:http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx

--edit

解決方案:

System.Diagnostics.PerformanceCounter theMemCounter = 
    new System.Diagnostics.PerformanceCounter("Process", "Working Set", 
    System.Diagnostics.Process.GetCurrentProcess().ProcessName); 

var memStart = theMemCounter.NextValue()/1024/1024; 
+0

這篇文章可能也有用:http://stackoverflow.com/questions/3411805/how-to-use-net-performancecounter-to-track-memory-and-cpu-usage-per-process – 2012-04-17 13:52:08