c#: PerformanceCounterCategory e PerformanceCounter per monitorare lo stato della memoria di un processo all’interno del CLR .NET
Con queste classi è possibile monitorare molti aspetti vitali delle nostre applicazioni, come utilizzo di memoria, i/o sul disco ecc..
Questo link della Microsoft contiene molta documentazione interessante.
Il codice che segue analizza lo stato della memoria relativa un singolo processo, corrispondente ad una applicazione .NET.
// Questa è l'area di cui vogliamo i contatori, ovvero ".NET CLR Memory" System.Diagnostics.PerformanceCounterCategory area = new System.Diagnostics.PerformanceCounterCategory(".NET CLR Memory"); |
Il seguente codice mostra a video tutte le istanze dentro l’area .NET CLR Memory analizzabili in quel momento sulla macchina:
string[] instanceNames; instanceNames = mycat.GetInstanceNames(); for (int i=0; i < instanceNames.Length; i++) { Console.WriteLine("Nome Istanza: {0}", instanceNames[i]); } |
Il suo output è simile a questo:
C:\Documents and Settings\colonnan\Documenti\Visual Studio 2008\Projects\Console Application3\ConsoleApplication3\bin\Debug>MonitorProcesso.exe Nome Istanza: <strong>MonitorProcesso</strong> Nome Istanza: devenv Nome Istanza: _Global_ Nome Istanza: MonitorProcesso.vshost |
In questo esempio "MonitorProcesso.exe"
è l’applicazione che sto lanciando, rappresentato dall’istanza "MonitorProcesso"
.
Per analizzare lo stato di memoria di MonitorProcesso.exe:
//Array dei contatori di un singolo processo. System.Diagnostics.PerformanceCounter[] counters = area.GetCounters("MonitorProcesso"); for (int i = 0; i < counters.Length;i++) { Console.WriteLine("Performance counter: {0} = {1}", counters[i].CounterName, counters[i].NextValue()); } |
Il cui risultato è:
Performance counter: # Gen 0 Collections = 1 Performance counter: # Gen 1 Collections = 0 Performance counter: # Gen 2 Collections = 0 Performance counter: Promoted Memory from Gen 0 = 271400 Performance counter: Promoted Memory from Gen 1 = 0 Performance counter: Gen 0 Promoted Bytes/Sec = 0 Performance counter: Gen 1 Promoted Bytes/Sec = 0 Performance counter: Promoted Finalization-Memory from Gen 0 = 0 Performance counter: Promoted Finalization-Memory from Gen 1 = 1320 Performance counter: Gen 0 heap size = 524288 Performance counter: Gen 1 heap size = 524480 Performance counter: Gen 2 heap size = 12 Performance counter: Large Object Heap size = 541560 Performance counter: Finalization Survivors = 0 Performance counter: # GC Handles = 35 Performance counter: Allocated Bytes/sec = 0 Performance counter: # Induced GC = 0 Performance counter: % Time in GC = 1,227221 Performance counter: Not Displayed = 0 Performance counter: # Bytes in all Heaps = 1131064 Performance counter: # Total committed Bytes = 1662976 Performance counter: # Total reserved Bytes = 3,354624E+07 Performance counter: # of Pinned Objects = 0 Performance counter: # of Sink Blocks in use = 0 |
Leave a Reply