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 929649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:12:22+00:00 2026-05-15T20:12:22+00:00

Summary I have written a process monitor command-line application that takes as parameters: The

  • 0

Summary

I have written a process monitor command-line application that takes as parameters:

  • The process name or process ID
  • A CPU Threshold percent.

What the program does, is watches all processes with the passed name or pid, and if their CPU usage gets over the threshold%, it kills them.

I have two classes:
ProcessMonitor and ProcessMonitorList

The former, wraps around System.Diagnostics.PerformanceCounter
The latter is an IEnumarable that allows a list-like structure of the former.

The problem

The program itself works fine, however if I watch the Memory Usage on Task Manager, it grows in increments of about 20kB per second. Note: the program polls the CPU counter through PerformanceCounter every second.

This program needs to be running on a heavily used server, and there are a great number of processes it is watching. (20-30).

Investigation So far

I have used PerfMon to monitor the Private Bytes of the process versus the Total number of Bytes in all Heaps and according to the logic presented in the article referenced below, my results indicate that while fluctuating, the value remains bounded within an acceptable range, and hence there is no memory leak:
Article

I have also used FxCop to analyze my code, and it did not come up with anything relevant.

The Plot Thickens

Not being comfortable with just saying, Oh then there’s no memory leak, I investigated further, and found (through debugging) that the following lines of code demonstrate where the leak is occurring, with the arrow showing the exact line.

_pc = new PerformanceCounter("Process", "% Processor Time", processName);

The above is where _pc is initiated, and is in the constructor of my ProcessMonitor class.

The below is the method that is causing the memory leak. This method is being called every second from my main.

public float NextValue()
{
        if (HasExited()) return PROCESS_ENDED;
        if (_pc != null)
        {
            _lastSample = _pc.NextValue();   //<-----------------------
            return _lastSample;
        }
        else return -1;
}

This indicates to me that the leak exists inside the NextValue() method, which is inside the System.Diagnostics.PerformanceCounter class.

My Questions:

  1. Is this a known problem, and how do I get around it?
  2. Is my assumption that the task manager’s memory usage increasing implies that there is indeed a memory leak correct?
  3. Are there any better ways to monitor multiple instances of a specific process and shut them down if they go over a specific threshold CPU usage, and then send an email?
  • 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-15T20:12:22+00:00Added an answer on May 15, 2026 at 8:12 pm

    So I think I figured it out.
    Using the Reflector tool, I was able to examine the code inside System.Diagnostics.

    It appears that the NextValue method calls

    GC.SuppressFinalization();
    

    This means that (I think, and please correct if I am wrong) that I needed to explicitly call Dispose() on all my classes.

    So, what I did is implement IDisposable on all of my classes, especially the one that wrapped around PerformanceCounter.

    I wrote more explicit cleanup of my IList<PerformanceMonitor>, and the internals,
    and voilà, the memory behavior changed.

    It oscillates, but the memory usage is clearly bounded between an acceptable range over a long period of time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written software which takes advantage of POSIX threads so that I can
Let's say that I have written a custom e-mail management application for the company
SUMMARY: I have an application which consumes way more memory that it should (roughly
Summary: I have a struct that is read/written to file. This struct changes frequently,
Summary: I have passed the name of the id select tag into an Array.
Summary I have looked over the code the SpiderMonkey 'shell' application uses to create
--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
I have written a method that downloads some files, and now I'm trying to
I have the following code that was written without Tests but is actually quite
I have written two methods that take and return a string. One converts from

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.