locked
PerformanceCounter somehow locks my dll... RRS feed

  • Question

  • Hi,

    I use this code from a Windows service:

    using (var c = new PerformanceCounter("Process", Working Set", instance, true))
       return c.RawValue;
    

    Which Works, no problems. The problems come when I shutdown the Windows service. My dll is locked for 4 seconds or so, after the service has shut Down. If I remove the PerformanceCounter lines I can delete the dll right after service shutdown.

    Is this normal?

    --
    Werner

    Thursday, June 6, 2013 12:47 PM

All replies

  • Can you use the Process.WorkingSet64 property instead of a new PerformanceCounter every time you want the value?
    • Edited by JohnWein Thursday, June 6, 2013 6:01 PM
    Thursday, June 6, 2013 5:59 PM
  • Hi John,

    No not really, because it doesn't reflect the values that tech people see in their monitoring software. The Process.WorkingSet64 value is nowhere near the values of the "Working Set - Private" and "Working Set" counters.

    Friday, June 7, 2013 6:05 AM
  • Hi John,

    No not really, because it doesn't reflect the values that tech people see in their monitoring software. The Process.WorkingSet64 value is nowhere near the values of the "Working Set - Private" and "Working Set" counters.

    Is this statement from the MSDN library incorrect?

    This property can be used to monitor memory usage on computers with 32-bit processors or 64-bit processors. The property value is equivalent to the Working Set performance counter for the process.

    Friday, June 7, 2013 7:09 AM
  • Is this statement from the MSDN library incorrect?

    This property can be used to monitor memory usage on computers with 32-bit processors or 64-bit processors. The property value is equivalent to the Working Set performance counter for the process.

    Well yes, consider this code tested on my 64 bit developer PC. Notice I included both counters. The "Working Set - Private" would be used on newer computers and the "Working Set" for older computers. So if you have an old computer, it might not have the "Working Set - Private" counter...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace PerformanceCounterTest1
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var process = Process.GetCurrentProcess())
                {
                    Console.WriteLine("Process.WorkingSet64 = {0}", process.WorkingSet64);
    
                    var instance = GetInstance(process.Id);
                    if (string.IsNullOrEmpty(instance))
                        throw new NullReferenceException("instance");
    
                    using (var counter = new PerformanceCounter("Process", "Working Set - Private", instance))
                        Console.WriteLine("Counter Working Set - Private = {0}", counter.NextValue());
    
                    using (var counter = new PerformanceCounter("Process", "Working Set", instance))
                        Console.WriteLine("Counter Working Set = {0}", counter.NextValue());
                }
    
                Console.ReadKey();
            }
    
            private static string GetInstance(int processId)
            {
                var processCategory = new PerformanceCounterCategory("Process");
                var runningInstances = processCategory.GetInstanceNames();
    
                foreach (var runnedInstance in runningInstances)
                {
                    using (var performanceCounter = new PerformanceCounter("Process", "ID Process", runnedInstance, true))
                    {
                        if ((int)performanceCounter.RawValue == processId)
                            return runnedInstance;
                    }
                }
    
                return null;
            }
        }
    }

    They are not the same.

    --

    Werner



    Friday, June 7, 2013 7:32 AM
  • Anyways, turning back on the performance counter locking. Anyone seen this before? Any "fix"?

    --
    Werner

    Monday, June 10, 2013 1:41 PM
  • Can you explain the scenario a little more? You say it's a Windows service, but you refer to a Dll being locked. Does that mean your performance counter code is in a Dll that is called from the service? So you shut down the service and it finishes but you cannot delete the Dll for 4 seconds, yes?


    Phil Wilson

    Monday, June 10, 2013 7:23 PM
  • Hi Phil,

    Yes that is the scenario right there. I fail to reproduce this outside the Windows service context. So perhaps the problem isn't with the performance counter, but Windows telling me - prematurely - that my service has stopped...

     


    Tuesday, June 11, 2013 11:22 AM
  • Some more information, I've tried to indentify what object causes the problem. And it turns out that just about any reference to Performance counters will show this oddness, for example I have stripped all Perf-logic from my service and put this in a class instantiated at service start:

    public class Test
    {
        public Test()
        {
            PerformanceCounterCategory.Exists("Process");
        }
    }

    Calling "new Test();" from the service start method Means that the dll is locked 2-3 seconds after the service is stopped. That is pretty weird.


    Tuesday, June 11, 2013 1:10 PM
  • Yet a new Development. Just tried the simpliest setup: New Windows service does not have this problem. So it boils Down to my dll being loaded (Assembly.Load) by the exe I guess.

    Perhaps not relevant to this specific forum anymore? Yet it definately is that single line of code that is locking...

    --
    Werner


    Tuesday, June 11, 2013 1:36 PM