Web Service - Event Handle Leak?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phang98
    New Member
    • Jul 2008
    • 5

    Web Service - Event Handle Leak?

    I wrote a simple web service with Helloworld web method using VS2005.
    By using the process exploere, I found every HelloWorld call with increase the 1 event handle in the w3wp.exe, and the event handle will stay forever, till the worker process recycle. Is it a common behavior? or I missing any dispose pattern?
  • mynkow
    New Member
    • Jul 2007
    • 37

    #2
    I had the same troubles like you. I have a WCF web service for graphic effects. Each time I transfer the image the RAM increased where the service is hosted. Then I simulated long and intensive work because I thought that the server will stop responding after some time. So the results were positive for me. The garbage collector frees the memory. But you should consider to do the following in the web service:
    - sometimes you have to dispose. Look carefully your code.
    - do not use the keyword "new" in loops.
    - simulate long and intensive work of your service.
    - use Process explorer instead windows task manager to track the memory

    If you wish you can call the garbage collector yourself. This code will be useful:

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    namespace Tools
    {
    	/// <summary>
    	/// Provides simple memory management.
    	/// </summary>
    	public sealed class Memory
    	{
    		/// <summary>
    		/// Reduces the memory usage of the current process by trimming its working set.
    		/// </summary>
    		public static void ReduceMemoryUsage()
    		{
    			GC.Collect();
    			GC.WaitForPendingFinalizers();
    
    			// Remove as many pages as possible from the working set of the current process
    			SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
    		}
    
    		[DllImport("kernel32.dll")]
    		private static extern bool SetProcessWorkingSetSize(IntPtr hProcess,
    			int minimumWorkingSetSize, int maximumWorkingSetSize);
    	}
    }

    Comment

    • phang98
      New Member
      • Jul 2008
      • 5

      #3
      "mynkow" - Thanks for you feedback.

      The actual problem I encountered actually is in the production environment where the "Event handle" count is keep increasing linearly (captured by perfmon and identified the type with Process Explorer and handle.exe) and reach more than 4000.

      My findings and facts:
      - the handles leak are event type handle.
      - the symptom is, the web service will stop responding after few weeks.
      - the web service worker process is configured to recycle every 26 hours.
      - the App pool is share by 2 web services. During one of the web service stop responding, the another web services still work properly.
      - Search through the code in the web services, there are no event was created or subscribe to it.

      Base on those facts, I started to create a simple hello world web service to see the pattern on event handle creation. The hello world web service is the simplest web method that only return hello world string as response. But every call to the web method, the event handle will increase by one constantly. I did a stress test with a client program to iteration the call to 1000. and the event handle did increase to 1000.

      I also suspect, this is due to the event HelloWorldAsyn and event HelloWorldCompl eted that automatic created by the VS2005 web service template. I try to subscribe to HelloWorldCompl eted from the client program many time but there is no visible effect at all.

      I'm not sure the calling the GC.collect () is a good practice or not. I'll try to test it up whether it can solve my problem or not. Thanks "mynkow".
      Hope this give u better picture on the problem, hope to hear your feedback again. :)

      Comment

      • phang98
        New Member
        • Jul 2008
        • 5

        #4
        The free memory code that you posted is really work.
        I just wonder why the event handle leak is happening in ASP.net web services call. Is this consider a bug?

        Originally posted by mynkow
        I had the same troubles like you. I have a WCF web service for graphic effects. Each time I transfer the image the RAM increased where the service is hosted. Then I simulated long and intensive work because I thought that the server will stop responding after some time. So the results were positive for me. The garbage collector frees the memory. But you should consider to do the following in the web service:
        - sometimes you have to dispose. Look carefully your code.
        - do not use the keyword "new" in loops.
        - simulate long and intensive work of your service.
        - use Process explorer instead windows task manager to track the memory

        If you wish you can call the garbage collector yourself. This code will be useful:

        Code:
        using System;
        using System.Runtime.InteropServices;
        
        namespace Tools
        {
        	/// <summary>
        	/// Provides simple memory management.
        	/// </summary>
        	public sealed class Memory
        	{
        		/// <summary>
        		/// Reduces the memory usage of the current process by trimming its working set.
        		/// </summary>
        		public static void ReduceMemoryUsage()
        		{
        			GC.Collect();
        			GC.WaitForPendingFinalizers();
        
        			// Remove as many pages as possible from the working set of the current process
        			SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
        		}
        
        		[DllImport("kernel32.dll")]
        		private static extern bool SetProcessWorkingSetSize(IntPtr hProcess,
        			int minimumWorkingSetSize, int maximumWorkingSetSize);
        	}
        }

        Comment

        • charan49
          New Member
          • Nov 2013
          • 1

          #5
          i am also facing the same issue, I have a WCF service hosted in windowds service, the Handlers count on the WCF is keep increasing every minute..


          Plese help

          Comment

          Working...