Archive

Posts Tagged ‘PerformanceCounter’

c#: PerformanceCounterCategory e PerformanceCounter per monitorare lo stato della memoria di un processo all’interno del CLR .NET

March 9th, 2010 Nicola No comments

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

c#: monitorare la Memoria di sistema

December 11th, 2009 Nicola No comments

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));