See Also: PerformanceCounterCategory Members
Creating or deleting a performance counter requires synchronization of the underlying code by using a named mutex. If a highly privileged application locks the named mutex, attempts to create or delete a performance counter causes the application to stop responding until the lock is released. To help avoid this problem, never grant System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode permission to untrusted code. In addition, System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode permission potentially allows other permissions to be bypassed and should only be granted to highly trusted code.
The System.Diagnostics.PerformanceCounterCategory instance's PerformanceCounterCategory.CategoryName property is displayed in the Performance Object field of the Performance Viewer application's Add Counter dialog box.
The System.Diagnostics.PerformanceCounterCategory class provides several methods for interacting with counters and categories on the computer. The erload:System.Diagnostics.PerformanceCounterCategory.Create methods enable you to define custom categories. The PerformanceCounterCategory.Delete(string) method provides a way to remove categories from the computer. The PerformanceCounterCategory.GetCategories method enables you to view the list of categories, while PerformanceCounterCategory.ReadCategory retrieves all the counter and instance data associated with a single category.
A performance counter publishes performance data about an application. Categories include physical components (such as processors, disks, and memory) and system objects (such as processes and threads). System counters that are related to the same performance object are grouped into a category that indicates their common focus. When you create an instance of the System.Diagnostics.PerformanceCounter class, you first indicate the category with which the component will interact, and then you choose a counter from that category.
For example, one Windows counter category is the Memory category. System counters within this category track memory data such as the number of bytes available and the number of bytes cached. If you wanted to work with the bytes cached in your application, you would create an instance of the System.Diagnostics.PerformanceCounter component, connect it to the Memory category, and then pick the appropriate counter (in this case, Cached Bytes) from that category.
Although your system makes many more counter categories available, the categories that you will probably interact with most frequently are the Cache, Memory, Objects, PhysicalDisk, Process, Processor, Server, System, and Thread categories.
The PerformanceCounter.RemoveInstance method in the System.Diagnostics.PerformanceCounter class will release the counter and, if the reuse option is selected for that category, the instance of the counter will be reused. This could cause a race condition if another process or even another part of the code is trying to write to the counter instance.
It is strongly recommended that new performance counter categories be created during the installation of the application, not during the execution of the application. This allows time for the operating system to refresh its list of registered performance counter categories. If the list has not been refreshed, the attempt to use the category will fail.
Performance counter categories installed with the dnprdnlong use separate shared memory, with each performance counter category having its own memory. You can specify the size of separate shared memory by creating a DWORD named FileMappingSize in the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<category name>\Performance. The FileMappingSize value is set to the shared memory size of the category. The default size is 131072 decimal. If the FileMappingSize value is not present, the fileMappingSize attribute value for the performanceCounters element specified in the Machine.config file is used, causing additional overhead for configuration file processing. You can realize a performance improvement for application startup by setting the file mapping size in the registry. For more information about the file mapping size, see <performanceCounters> Element.