1

I'm trying to use a Memory Performance Counter:

System.Diagnostics.PerformanceCounter theMemCounter = 
    new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes",
    System.Diagnostics.Process.GetCurrentProcess().ProcessName, true);

var memStart = theMemCounter.NextValue();

But in the second line i'm getting the following error:

Counter is single instance, instance name 'WebDev.WebServer40' is not valid for this counter category.

What is the problem?

Vinicius Ottoni
  • 4,631
  • 9
  • 42
  • 64

1 Answers1

2

Ottoni, I don't think you can specify a process to this particular Performance Counter, since it monitors the available memory on the whole system.

Maybe the perfcounter you're looking for is ".NET CLR Memory(INSTANCE)# Bytes in all Heaps" or some other in the .NET CLR Memory category, which is able to monitor memory usage for all or a specified .net application.

More info on this category here: http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx

--EDIT

Solution:

System.Diagnostics.PerformanceCounter theMemCounter =
    new System.Diagnostics.PerformanceCounter("Process", "Working Set",
    System.Diagnostics.Process.GetCurrentProcess().ProcessName);

var memStart = theMemCounter.NextValue() / 1024 / 1024;
Vinicius Ottoni
  • 4,631
  • 9
  • 42
  • 64
  • this post can be useful too: http://stackoverflow.com/questions/3411805/how-to-use-net-performancecounter-to-track-memory-and-cpu-usage-per-process – Amirton Chagas Apr 17 '12 at 13:52