﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Robert,
I find it highly unlikely that a single ManualResetEvent would be that expensive.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment25</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment25</guid><pubDate>Mon, 25 Jul 2011 15:19:00 GMT</pubDate></item><item><title>Robert Slaney commented on High memory usage with WCF Discovery</title><description>Just looked through the System.ServiceModel.Discovery assembly using reflector and the Find method on DiscoveryClient instantiates a SyncOperationState object which internally contains ManualResetEvent.

SyncOperationState class is internal but not disposable, and I cannot see anywhere that the ManualResetEvent is closed or disposed.

I suppose the GC will eventually get around to collecting this but would definitely contribute to the excessive memory

</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment24</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment24</guid><pubDate>Sun, 24 Jul 2011 23:11:41 GMT</pubDate></item><item><title>Bertrand Le Roy commented on High memory usage with WCF Discovery</title><description>Just FYI, the post looks all funny in Google Reader, apparently doubly-encoded.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment23</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment23</guid><pubDate>Sat, 23 Jul 2011 05:59:56 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>That is very interesting, because it looks like most of the memory cost is actually on the Service Host, not on the DiscoverClient.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment22</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment22</guid><pubDate>Sat, 23 Jul 2011 02:25:32 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Remco,
I am probably going to try that next, yes. But I think that the actual cost is per connected server, need to check that further</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment21</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment21</guid><pubDate>Sat, 23 Jul 2011 02:23:50 GMT</pubDate></item><item><title>Remco commented on High memory usage with WCF Discovery</title><description>It's probably the Binding buffer configuration.

Using the below UdpDiscoveryEndpoint configuration for both service and client there is substantially less memory usage (from 170MB to 67MB maximum working set).
Also,  discovery is slower.

            var endpoint = new UdpDiscoveryEndpoint();
            var binding = (CustomBinding)endpoint.Binding;
            var element = binding.Elements.OfType&lt;TransportBindingElement&gt;().First();
            element.MaxBufferPoolSize = ushort.MaxValue;
            element.MaxReceivedMessageSize = ushort.MaxValue / 4;
            return endpoint;
</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment20</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment20</guid><pubDate>Fri, 22 Jul 2011 21:59:20 GMT</pubDate></item><item><title>Remco commented on High memory usage with WCF Discovery</title><description>Ah yes, well, can't help you there. apparently 640K is not enough for even 1 discoveryclient.

Maybe it behaves better memorywise if just 1 discovery client with multiple async discoveries in flight is used instead of a whole bunch of clients? I am unfamiliar with System.ServiceModel.Discovery so I'm just guessing here.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment19</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment19</guid><pubDate>Fri, 22 Jul 2011 20:49:59 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Remco,
My problem isn't so much that this is a memory leak, my problem is that it is using _so much_ memory.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment18</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment18</guid><pubDate>Fri, 22 Jul 2011 20:08:56 GMT</pubDate></item><item><title>Remco commented on High memory usage with WCF Discovery</title><description>Last spam of the day, just edited the full repro to include an extra sleep of 60 seconds. Verdict: Select isn't broken, it's garbage collector is slow ;-)</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment17</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment17</guid><pubDate>Fri, 22 Jul 2011 20:05:44 GMT</pubDate></item><item><title>Remco commented on High memory usage with WCF Discovery</title><description>It gets weirder, the results aren't consistent.

only if I add this bit of code before the console.readkey I get a consistent drop of memory usage to about 40 MB

            Thread.Sleep(60000);
            PrintMemory("After sleep");
            GC.Collect();
            PrintMemory("After collect 2 ");
            GC.WaitForPendingFinalizers();
            PrintMemory("After finalizers 2");
            Console.ReadKey();

So my guess there's something fishy going on in the unmanaged world.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment16</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment16</guid><pubDate>Fri, 22 Jul 2011 19:49:07 GMT</pubDate></item><item><title>Remco commented on High memory usage with WCF Discovery</title><description>Maybe it's a debugger interaction.
running the sample outside of the debugger got me these results
with a debugger attached i got 209 for all the results.

Before collect:209,000
After collect:40,000
After finalizers:40,000

so select isn't broken. the debugger is?

full code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.ServiceModel.Discovery;
    using System.ServiceModel;

    namespace ConsoleApplication1
    {
        class Program
        { 
            static void Main(string[] args)
            {
                int count = 150;
                var countdown = new CountdownEvent(count);

                for (int i = 0; i &lt; count; i++)
                {
                    var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()); 
                    var findCriteria = new FindCriteria(typeof(IDiscoverableService))
                    {
                    };
                    discoveryClient.FindProgressChanged += (sender, eventArgs) =&gt; { }; // do nothing
                    discoveryClient.FindCompleted += (sender, eventArgs) =&gt;
                    {
                        using (discoveryClient)
                            discoveryClient.Close();

                        PrintMemory(countdown.CurrentCount.ToString());
                        countdown.Signal();
                    };
                    discoveryClient.FindAsync(findCriteria);
                }

                countdown.Wait();

                PrintMemory("Before collect");
                GC.Collect();
                PrintMemory("After collect");
                GC.WaitForPendingFinalizers();
                PrintMemory("After finalizers");
                Console.ReadKey(); 
            }

            private static void PrintMemory(string p)
            {
                Console.WriteLine("{0}:{1:N3}", p, Environment.WorkingSet / (1024 * 1024));
            }
        }
        [ServiceContract]
        public interface IDiscoverableService
        {
            [OperationContract]
            void dosomething();
        }
    }</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment15</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment15</guid><pubDate>Fri, 22 Jul 2011 19:30:42 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Kerry,
I would hope so, I am disposing on it directly afterward, at any rate.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment14</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment14</guid><pubDate>Fri, 22 Jul 2011 19:23:15 GMT</pubDate></item><item><title>Kerry commented on High memory usage with WCF Discovery</title><description>This may be simplistic, but is the "discoveryClient.Find(findCriteria)" method disposing of its results after returning? </description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment13</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment13</guid><pubDate>Fri, 22 Jul 2011 19:20:44 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Remco,
Even so, 200MB still seems to be very excessive amount, no?</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment12</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment12</guid><pubDate>Fri, 22 Jul 2011 18:38:51 GMT</pubDate></item><item><title>Remco commented on High memory usage with WCF Discovery</title><description>Really strange,  the second example levels out at 210MB on my box 
(Vista 64bit),  with no config of WCF whatsoever.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment11</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment11</guid><pubDate>Fri, 22 Jul 2011 18:34:41 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Remco,
I have done that with dotTrace, I can see that most of the memory is byte[], held by WCF, doesn't really help me figure out what is going on.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment10</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment10</guid><pubDate>Fri, 22 Jul 2011 18:23:07 GMT</pubDate></item><item><title>Remco commented on High memory usage with WCF Discovery</title><description>How about bringing out the big guns:
procdump from sysinternals to make a dump,
and windbg with the psscor4 extensions to show you where the memory has gone. ( with !dumpheap -stat )
</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment9</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment9</guid><pubDate>Fri, 22 Jul 2011 18:21:38 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Saj,
Nope, I am calling Close there.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment8</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment8</guid><pubDate>Fri, 22 Jul 2011 17:24:07 GMT</pubDate></item><item><title>Saj commented on High memory usage with WCF Discovery</title><description>I could be wrong but could you try
using ( var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()) )
{
    var findCriteria = new FindCriteria(typeof(IDiscoverableService))
    {
        Duration = TimeSpan.FromSeconds(1)
    };
    discoveryClient.Find(findCriteria);
    discoveryClient.Close();
}</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment7</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment7</guid><pubDate>Fri, 22 Jul 2011 17:22:31 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Vadim,
Thanks, I wouldn't want you to "waste" this card unless you really don't have anything else to do with it.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment6</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment6</guid><pubDate>Fri, 22 Jul 2011 17:22:10 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Jon,
Interesting observation, and I tried playing with those values, but I don't think that this is it, I am seeing the same numbers, pretty much, even after limiting the max pool size to just 512 (bytes!).</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment5</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment5</guid><pubDate>Fri, 22 Jul 2011 17:21:34 GMT</pubDate></item><item><title>Jon commented on High memory usage with WCF Discovery</title><description>Funny enough I was looking at this problem last week about wcf service holding onto large amounts of memory it turned out to be a simple configuration change to the setting maxBufferPoolSize.  When you look at a lot of examples about sending large files etc through wcf services everyone blindly sets all the settings (maxBufferSize, maxReceivedMessageSize etc) to huge values and ussually the maxBufferPoolSize get set to the same huge value.  However, this value indicates the pool size not a buffer size. so you want to limit your buffer pool to a number of cached buffers and not let it run wild.  Set the maxBufferPoolSize to 1 just to test this theory and your memory usage should drop dramtically (if it is this!).

I posted something on stackoverflow about this http://stackoverflow.com/questions/6713180/addressalreadyinuseexception-when-using-custombinding-but-not-when-using-nettcpbi/6720923#6720923

hope this helps!

</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment4</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment4</guid><pubDate>Fri, 22 Jul 2011 16:06:15 GMT</pubDate></item><item><title>Vadim commented on High memory usage with WCF Discovery</title><description>As an MS intern, I have an MS Q&amp;A tech support card. Tell me if you need one :)</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment3</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment3</guid><pubDate>Fri, 22 Jul 2011 15:38:35 GMT</pubDate></item><item><title>Ayende Rahien commented on High memory usage with WCF Discovery</title><description>Patric,
But I am not holding any reference to the DiscoveryClient in the event handlers, or anywhere else.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment2</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment2</guid><pubDate>Fri, 22 Jul 2011 14:28:20 GMT</pubDate></item><item><title>Patric commented on High memory usage with WCF Discovery</title><description>Fore some time ago I have a problem with objects that was using events and the original object (discoveryClient) cant be released and disposed. The problem this time was that the object was only referenced by event-handler and because of that you have a reference to the object and the GC was not able to discard the object. After remove the event-handler after the object was used fixed my memory-leak problem.</description><link>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment1</link><guid>http://ayende.com/67585/high-memory-usage-with-wcf-discovery#comment1</guid><pubDate>Fri, 22 Jul 2011 11:52:25 GMT</pubDate></item></channel></rss>