2

This is the code, I am trying to run to get Memory usage.

int main(array<System::String ^> ^args)
{
    PerformanceCounter^ ramCounter;
    try
    {
        Console::WriteLine(L"Hello World");
        ramCounter = gcnew PerformanceCounter("Memory", "Available MBytes");
        Console::WriteLine(L"Memory usgae:"+ramCounter->NextValue()+L"MB");
    }
    catch(Exception^ e)
    {
        Console::WriteLine("Error Message: "+e->Message);
        Console::WriteLine(e->StackTrace);
    }
    finally
    {
        if(ramCounter!=nullptr)
        {
            ramCounter->Close();
        }
        Console::WriteLine("Press any key to exit");
        Console::ReadLine();
    }
    return 0;
}

But I am getting exception like

Error Message: Category does not exist.

Error Message Stack trace:
at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter) at System.Diagnostics.PerformanceCounter.InitializeImpl() at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly) at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName) at main(String[] args) in c:\users\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp:line 31

Any idea about this..

Community
  • 1
  • 1
samba
  • 323
  • 1
  • 4
  • 14

1 Answers1

2

This might by localization issue. On my machine your code isn't working either, because its language is German. Hence I had to create the counter with

 gcnew PerformanceCounter("Arbeitsspeicher", "Verfügbare MB")

You can find the localized name by browsing the available performance counters in the Performance Monitor.

There also is a way to retrieve the counter data in a language-independent way. See this answer for details.

Community
  • 1
  • 1
Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65