c#: monitorare la Memoria di sistema
Instanziamo la classe: System.Diagnostics.PerformanceCounterCategory sulla categoria “Memory”:
System.Diagnostics.PerformanceCounterCategory memoria = new
System.Diagnostics.PerformanceCounterCategory("Memory");
Questa categoria non contiene istanze in realtime da monitorare, quindi l’array restituito da
momeria.GetInstanceNames();
non contiene elementi.
Per avere l’elenco di tutti i counters appartenenti alla categoria è sufficiente il ciclo:
System.Diagnostics.PerformanceCounter[] counters = memoria.GetCounters();
for (int i = 0; i < counters.Length;i++)
{
Console.WriteLine("Performance counter: {0} = {1}", counters[i].CounterName, counters[i].NextValue());
}
Un esempio del risultato:
Performance counter: Page Faults/sec = 0
Performance counter: Available Bytes = 8,281293E+07
Performance counter: Committed Bytes = 7,408845E+08
Performance counter: Commit Limit = 1,716732E+09
Performance counter: Write Copies/sec = 0
Performance counter: Transition Faults/sec = 0
Performance counter: Cache Faults/sec = 0
Performance counter: Demand Zero Faults/sec = 0
Performance counter: Pages/sec = 0
Performance counter: Pages Input/sec = 0
Performance counter: Page Reads/sec = 0
Performance counter: Pages Output/sec = 0
Performance counter: Pool Paged Bytes = 8,963277E+07
Performance counter: Pool Nonpaged Bytes = 1,693696E+07
Performance counter: Page Writes/sec = 0
Performance counter: Pool Paged Allocs = 74229
Performance counter: Pool Nonpaged Allocs = 55166
Performance counter: Free System Page Table Entries = 185494
Performance counter: Cache Bytes = 3,138232E+08
Performance counter: Cache Bytes Peak = 3,642819E+08
Performance counter: Pool Paged Resident Bytes = 8,944026E+07
Performance counter: System Code Total Bytes = 1114112
Performance counter: System Code Resident Bytes = 1990656
Performance counter: System Driver Total Bytes = 8232960
Performance counter: System Driver Resident Bytes = 1163264
Performance counter: System Cache Resident Bytes = 2,212291E+08
Performance counter: % Committed Bytes In Use = 43,19485
Performance counter: Available KBytes = 80004
Performance counter: Available MBytes = 78
Se si vuole il valore di un unico counter si può utilizzare un’altro costruttore di PerformanceCounter (http://physicus.blogspot.com/2006/12/real-time-ram-usage-with-c.html):
PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes");
Console.WriteLine("{0:0} Bytes available ({1:0} kB \t {2:0} MB)", byteCount, byteCount / 1024, byteCount / (1024 *
1024));
Leave a Reply