상세 컨텐츠

본문 제목

G e t t i n g S t a r t e d W i t h t h e P e r f o r m a n c e C o u n t e r s

.Net General

by 탑~! 2011. 10. 12. 13:07

본문

This article introduces the concept of Windows OS performance counter objects and provides details about .NET Framework performance counters.

What are Performance Counters?

Performance Counters are Windows OS objects that capture metrics about the performance of hardware and applications. For example, performance counters can capture performance metrics for processors, memory, threads, events, and processes. Metrics can be used to detect problems or to “tune” applications and hardware for maximum performance.

Performance Counters are named and grouped into peformance counter categories such as Processor, System, Thread, ASP.NET, .NET CLR Memory, and .NET LocksAndThreads. Within each category are individual performance counters. For example, within the .NET CLR Memory performance counter category are performance counters such as # Bytes in all heaps, Gen 0 heap size, # GC handles, and % time in GC.

Performance Counter objects may be accessed programmatically or with the Windows Performance Monitor Application (PerfMon).

If you are not familiar with performance counters or the Microsoft Performance Monitor applicaton, spend an hour with the Microsoft Performance Monitor to get familiar with the basics before you continue with this article. The Performance Monitor (PerfMon) is a performance object viewer that comes with Windows NT, Windows 2000, and Windows XP. PerfMon allows you to select performance counters from performance counter categories and display them in a variety of formats including graph, histogram, or report format. To start PerfMon: From the Windows Start menu choose Run, type in PerfMon, and click the OK button to start PerfMon. Click here for a good article on getting started with PerfMon.

.Net Framework Performance Counters

There are performance counters you can use to collect metrics about performance within the Common Language Runtime (CLR) and ASP.NET.

There are nine performance monitor categories for the CLR and two for ASP.NET as shown in the table below.

Common Language Runtime (CLR) Performance Counter Categories (9)

Category

Description

Exceptions

Exceptions metrics.

Interop

Metrics about interaction with COM components, COM+ services, and type libraries.

JIT

Metrics about JIT operations.

Loading

Metrics about assembly, class, and AppDomain loading.

Locks and Threads

Metrics about managed locks and threads.

Memory

Metrics about memory including the GC, and handles.

Networking

Metrics about networking.

Remoting

Metrics about remoting.

Security

Metrics about security checks the CLR performs.

ASP.NET Performance Counter Categories (2)

Category

Description

ASP.NET

Metrics about restarts and requests queued.

ASP.NET Applications

Metrics about worker thread restarts, errors, requests per second, and instance counts.

Click here to learn about the .NET Framework performance counters available in each category shown above.

Code Example One

The code example below introduces the use of performance counter objects in VB.NET code. This example uses a performance counter “Processes” from the performance counter category “System”. The “Processes” performance counter collects the number of processes running in the computer at the time of data collection.

' Get number of processes running in a computer.

' 1. Declare a variable of type PerformanceCounter named processessPerformanceCounter.

' 2. Call the PerformanceCounter New constructor passing it

' the "System" performance counter cateory and the "Processes" performance counter.

' 3. Assign the performance counter object returned from the New constructor

' to the processessPerformanceCounter variable.

Dim processesPerformanceCounter As New PerformanceCounter("System", "Processes")

' Call the performance counter's RawValue method to collect the number of

' processes (programs) running in the computer.

Console.WriteLine("Processes = " & processesPerformanceCounter.RawValue.ToString)

Result: Processes = 47

Next issue - how to use performance counters to monitor the performance of the .NET CLR memory used by an individual VB.NET application.

Next the article will look at a practical approach to using performance counters to check for memory and handle “leaks” during program development.

Please carefully examine the code example below this paragraph. It shows one way you can use performance counters to take a ‘snap shot’ of memory and unmanaged native handles which are being used by an executing VB.NET application at a point in time.

Public Sub ReadKeyMemoryAndHandlePerformanceCounters()

' Declare a variable of type String named applicationInstance.

Dim applicationInstance As String

' Call the Assembly GetExecutingAssembly method to get

' the name of this application. The first 15 characters of the

' application name will be what the Windows OS

' sees as the instance name unless you are running multiple instances

' of your application.

applicationInstance = _

Reflection.Assembly.GetExecutingAssembly.GetName.ToString.Substring(0, 15)

' Declare a variable of type ArrayList named performanceCounters.

' Use a ArrayList constructor to create a new ArrayList object.

' Assign the address (reference) of the new object

' to the performanceCounters variable.

Dim performanceCounters As New ArrayList()

' Instantiate the PeformanceCounters that can indicate

' memory and handle performance issues. Add each PerformanceCounter

' to the performanceCounters ArrayList as it is instantiated.

' # bytes in all heaps

performanceCounters.Add(New PerformanceCounter _

(".NET CLR Memory", "# bytes in all heaps", applicationInstance))

' # GC Handles

performanceCounters.Add(New PerformanceCounter _

(".NET CLR Memory", "# GC Handles", applicationInstance))

' Gen 0 heap Size

performanceCounters.Add(New PerformanceCounter _

(".NET CLR Memory", "Gen 0 Heap Size", applicationInstance))

' Gen 1 heap Size

performanceCounters.Add(New PerformanceCounter _

(".NET CLR Memory", "Gen 1 heap Size", applicationInstance))

' Gen 2 heap Size

performanceCounters.Add(New PerformanceCounter _

(".NET CLR Memory", "Gen 2 heap Size", applicationInstance))

' Large Object heap size

performanceCounters.Add(New PerformanceCounter _

(".NET CLR Memory", "Large Object Heap size", applicationInstance))

' Declare a variable of type StringBuilder named counterSnapshot.

' Use a StringBuilder constructor to create a new

' StringBuilder object. Assign the address (reference) of the

' new object to the counterSnapshot variable.

Dim counterSnapshot As New System.Text.StringBuilder()

' Loop through the PerformanceCounters

' in performanceCounters ArrayList.

Dim typePerformanceCounter As PerformanceCounter

For Each typePerformanceCounter In performanceCounters

' Append the PerformanceCounter's name and

' its Value to the counterSnapshot.

counterSnapshot.Append(typePerformanceCounter.CounterName.ToString _

& " " & GetCounterValue(typePerformanceCounter).ToString() & vbCrLf)

Next

' Write the counterSnapshot to the Console.

Console.WriteLine(counterSnapshot.ToString)

End Sub

Private Function GetCounterValue _

(ByVal pPerformanceCounter As PerformanceCounter) As String

' Retrieve PerformanceCounter result based on its CounterType.

' Learn more about CounterType by clicking here.

Select Case pPerformanceCounter.CounterType

Case PerformanceCounterType.NumberOfItems32,_

PerformanceCounterType.NumberOfItems64

Return pPerformanceCounter.RawValue.ToString

Case PerformanceCounterType.RateOfCountsPerSecond32, _

PerformanceCounterType.RateOfCountsPerSecond64

Return pPerformanceCounter.NextValue.ToString

Case PerformanceCounterType.AverageTimer32

Return pPerformanceCounter.NextValue.ToString

End Select

End Function

Result:

# bytes in all heaps 1333180

# GC Handles 224

Gen 0 Heap Size 1016120

Gen 1 heap Size 243456

Gen 2 heap Size 61284

Large Object Heap size 12320

How to Use the GetCounters Function

Over time, as a healthy application runs, each of the performance counters reported by the function should 1) increment, indicating use of memory and handles, and; 2) decrement, indicating return of memory and handles after use. Sustained growth of memory may indicate a memory leak. Sustained growth of handles may indicate a failure to release unmanaged resources after use.

The purpose of the GetCounters function is to provide a way for you to gather “snap shots” of the memory and handle performance counters during application runtime. In your application, from time to time, you could call the function and write the result it returns to a text file along with a description of where and when the “snap shot” was taken. After a run you could read the text file to see if memory and handles are being used and released or, used and held.

A full explanation of the meaning of these counters is outside the scope of this article. A description of the counters used is provided below along with some links to information that will help you understand potential heap memory and unmanaged handle problems that can occur when using the .NET Common Language Runtime.

Performance Counter

Description

# GC Handles

This counter displays the current number of GC Handles in use. GCHandles are handles to resources external to the CLR and the managed environment. Handles occupy small amounts of memory in the GCHeap but potentially expensive unmanaged resources.

# Bytes in all heaps

This counter is the sum of four other counters; Gen 0 Heap Size; Gen 1 Heap Size; Gen 2 Heap Size and the Large Object Heap Size.

# Gen 0 heap size

This counter displays the size (in bytes) of allocations that would trigger the next Gen 0 GC. This counter is updated at the end of a GC; its not updated on every allocation.

# Gen 1 heap size

This counter displays the current number of bytes in generation 2 (Gen 2). Objects are not directly allocated in this generation; they are promoted from Gen 1 during previous Gen 1 GCs. This counter is updated at the end of a GC; its not updated on every allocation.

# Gen 2 heap size

This counter displays the current number of bytes in generation 2 (Gen 2). Objects are not directly allocated in this generation; they are promoted from Gen 1 during previous Gen 1 GCs. This counter is updated at the end of a GC; its not updated on every allocation.

Large Object Heap Size

This counter displays the current size of the Large Object Heap in bytes. Objects greater than 20 KBytes are treated as large objects by the Garbage Collector and are directly allocated in a special heap; they are not promoted through the generations. This counter is updated at the end of a GC; its not updated on every allocation.

Learn more about performance counters by clicking here and here.

Automatic Memory Management in .NET

Debugging Memory Problems

Manage Unmanaged Resources and Memory With VB.NET


[출처] http://www.getdotnetcode.com/gdncstore/free/Articles/Getting%20Started%20With%20Performance%20Counters.htm

'.Net General' 카테고리의 다른 글

Google APIs Client Library for .NET  (0) 2012.03.16
[C#] 무료 .NET 라이브러리 리스트  (0) 2011.11.17
관리 코드에서 메모리 누수 확인 및 방지  (0) 2011.10.12
GAC 파일 Copy 하기  (0) 2011.08.22
코드 난독화 4  (0) 2011.06.15

관련글 더보기