Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7187539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:52:07+00:00 2026-05-28T18:52:07+00:00

According to How to use .NET PerformanceCounter to track memory and CPU usage per

  • 0

According to How to use .NET PerformanceCounter to track memory and CPU usage per process? PerformanceCounter should give me the number of memory usage of a given process.

According to MSDN, Process instance may also give me more or less the same number.

In order to verify my assumptions, I wrote the following code:

class Program
{
    static Process process = Process.GetCurrentProcess();

    static PerformanceCounter privateBytesCounter = new PerformanceCounter("Process", "Private Bytes", process.ProcessName);
    static PerformanceCounter workingSetCounter = new PerformanceCounter("Process", "Working Set", process.ProcessName);

    static void Main(string[] args)
    {


        GetMeasure();

        Console.WriteLine("\nPress enter to allocate great amount of memory");
        Console.ReadLine();
        int[] arr = new int[10000000];
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = i;
        }

        GetMeasure();

        privateBytesCounter.Dispose();
        workingSetCounter.Dispose();
        Console.ReadKey();
    }

    private static void GetMeasure()
    {
        Console.WriteLine("{0,38} {1,20}", "Private bytes", "working set");
        Console.WriteLine("process data{0,23} {1,20}", process.PrivateMemorySize64 / 1024, process.WorkingSet64 / 1024);
        Console.WriteLine("PerformanceCounter data{0,12} {1,20}", privateBytesCounter.NextValue() / 1024, workingSetCounter.NextValue() / 1024);
    }

}

The output looks like

                         Private bytes          working set
process data                  22880                17516
PerformanceCounter data       21608                15608

Press enter to allocate great amount of memory

                         Private bytes          working set
process data                  22880                17516
PerformanceCounter data       21608                15608

Exactly the same! In the contrast, private bytes shown in Process Explorer increased from 32732 to 63620.

So am I doing something wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:52:08+00:00Added an answer on May 28, 2026 at 6:52 pm

    You have to tell your process instance it should refresh its cached data. Data is not gathered each time you access to a property for performance purposes. You have to manually demand the data update.

    private static void GetMeasure()
    {
        process.Refresh();  // Updates process information
    
        Console.WriteLine("{0,38} {1,20}", "Private bytes", "working set");
        Console.WriteLine("process data{0,23} {1,20}", process.PrivateMemorySize64 / 1024, process.WorkingSet64 / 1024);
        Console.WriteLine("PerformanceCounter data{0,12} {1,20}", privateBytesCounter.NextValue() / 1024, workingSetCounter.NextValue() / 1024);
    }
    

    That’s for your process.
    For performance counters, NextValue() is supposed to retrieve a new fresh data each time, so I can’t explain why it doesn’t on your machine. On mine it works fine.

    EDIT:

    With the process.Refresh() added, here’s what I get:

                             Private bytes          working set
    process data                  25596                22932
    PerformanceCounter data       26172                23600
    
    Press enter to allocate great amount of memory
                             Private bytes          working set
    process data                  65704                61848
    PerformanceCounter data       65828                61880
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to [MSDN: Array usage guidelines]( http://msdn.microsoft.com/en-us/library/k2604h5s(VS.71).aspx) : Array Valued Properties You should use
According to php.net I should use mysql_real_escape_string() and turn off magic quotes , because
According to MSDN : If you did not use the Start method to start
According to this http://perldoc.perl.org/UNIVERSAL.html I shouldn't use UNIVERSAL::isa() and should instead use $obj->isa() or
According to this page, it's possible to use TClientDataset as an in-memory dataset, completely
According to the Script# website (http://projects.nikhilk.net/ScriptSharp), You can also use Visual C# Express. I've
I use ListView in asp.net. I get data from provider table. According providerId image
I can't figure out how to use the .NET class: System.Globalization.CultureInfo . According to
I understand how to use Events according to Net Framework guidelines, but what are
Is it possible to use JSON.NET as default JSON serializer in ASP.NET MVC 3? According to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.