1

我们有一个 WCF 客户端,我们需要在发送之前保存请求,但是在每次序列化之后,都会有一些泄露的 windows 事件句柄。我们已经尝试过 windbg,但句柄是由 clr 创建的。使用 sysinternals handles.exe 显示信号量和事件在不断增加,非托管内存也在增加:

Handle type summary:
  ALPC Port       : 10
  Desktop         : 1
  Directory       : 5
  EtwRegistration : 16
  Event           : 574
  File            : 12
  IoCompletion    : 3
  Key             : 13
  KeyedEvent      : 1
  Mutant          : 7
  Process         : 1
  Section         : 11
  Semaphore       : 467
  Thread          : 19
  Timer           : 3
  TpWorkerFactory : 16
  WindowStation   : 2
Total handles: 1161

经过一些测试,似乎该行为仅发生在 4.0/4.5 以下是演示问题的测试代码:

namespace HandleLeak
{
    class Program
    {
        private static XDocument SerializeToSoap(object source)
        {
            TypedMessageConverter messageConverter = TypedMessageConverter.Create(source.GetType(), null, new XmlSerializerFormatAttribute());
            using (Message request = messageConverter.ToMessage(source, MessageVersion.Soap11))
            {
                var xdoc = new XDocument();
                using (var wr = xdoc.CreateWriter())
                {
                    request.WriteMessage(wr);
                }
                return xdoc;
            }
        }

        static void Main(string[] args)
        {
            var sr = new SomeRequest();
            while(true)
            {
                SerializeToSoap(sr);
                GC.Collect();

                var currentProcess = Process.GetCurrentProcess();
                Console.WriteLine("Handles: {0}", currentProcess.HandleCount);
                Console.WriteLine("press any key to continue, esc to quit");
                if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    break;
            }
            Console.WriteLine("Done");
            Console.ReadKey();
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://test")]
    public partial class SomeType
    {

    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
    public partial class SomeRequest
    {

        [System.ServiceModel.MessageBodyMemberAttribute(Name = "someRequest", Namespace = "http://test", Order = 0)]
        public SomeType statusRequest1;

        public SomeRequest()
        {
        }

        public SomeRequest(SomeType statusRequest1)
        {
            this.statusRequest1 = statusRequest1;
        }
    }
}

问题是,我们做错了什么,还是框架中的错误?

4

1 回答 1

3

Well, it turns out I should have googled a bit more:

http://www-jo.se/f.pfleger/memoryleak and http://plainoldstan.blogspot.ch/2011/04/wcf-memory-leak-with.html

The solution is to cache the TypedMessageConverter, who would have guessed.

于 2012-11-13T14:55:43.727 回答