locked
Out of memory exception in C# WCF service RRS feed

  • Question

  • I've a C# wcf service in which my GetProducts() method has code as per below.

        using (var client = CreateHttpClient("/myURL", string.Empty))
                    {
                        HttpResponseMessage response = client.GetAsync(client.BaseAddress.AbsolutePath).Result;
        
                        if (response.IsSuccessStatusCode)
                        {
                            var products= response.Content.ReadAsAsync<List<Product>>().Result;
                            lstProducts.AddRange(products);
                        }
                        else
                        {
                            throw new Exception("Error fetching products...");
                        }
                    }

    There's another method A() in this service that calls this method and it throws following out of memory exception:

        System.AggregateException: One or more errors occurred. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
           at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
           at System.Threading.Thread.Start(StackCrawlMark& stackMark)
           at System.Threading.Thread.Start()
           at System.Net.TimerThread.Prod()
           at System.Net.TimerThread.TimerQueue.CreateTimer(Callback callback, Object context)
           at System.Net.ServicePoint..ctor(Uri address, Queue defaultIdlingQueue, Int32 defaultConnectionLimit, String lookupString, Boolean userChangedLimit, Boolean proxyServicePoint)
           at System.Net.ServicePointManager.FindServicePointHelper(Uri address, Boolean isProxyServicePoint)
           at System.Net.ServicePointManager.FindServicePoint(Uri address, IWebProxy proxy, ProxyChain& chain, HttpAbortDelegate& abortDelegate, Int32& abortState)
           at System.Net.HttpWebRequest.FindServicePoint(Boolean forceFind)
           at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
           at System.Net.Http.HttpClientHandler.StartGettingResponse(RequestState state)
           at System.Net.Http.HttpClientHandler.StartRequest(Object obj)
           --- End of inner exception stack trace ---
           at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
           at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
           at System.Threading.Tasks.Task`1.get_Result()
        MyService.ProductsManager.GetProducts()
        --> (Inner Exception #0) System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
           at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
           at System.Threading.Thread.Start(StackCrawlMark& stackMark)
           at System.Threading.Thread.Start()
           at System.Net.TimerThread.Prod()
           at System.Net.TimerThread.TimerQueue.CreateTimer(Callback callback, Object context)
           at System.Net.ServicePoint..ctor(Uri address, Queue defaultIdlingQueue, Int32 defaultConnectionLimit, String lookupString, Boolean userChangedLimit, Boolean proxyServicePoint)
           at System.Net.ServicePointManager.FindServicePointHelper(Uri address, Boolean isProxyServicePoint)
           at System.Net.ServicePointManager.FindServicePoint(Uri address, IWebProxy proxy, ProxyChain& chain, HttpAbortDelegate& abortDelegate, Int32& abortState)
           at System.Net.HttpWebRequest.FindServicePoint(Boolean forceFind)
           at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
           at System.Net.Http.HttpClientHandler.StartGettingResponse(RequestState state)
           at System.Net.Http.HttpClientHandler.StartRequest(Object obj)<---

    I'm already using the "using" block in GetProducts() method.So just wondering what else I could do in my code to prevent this out of memory exception?
    Or could it be that there is a memory leak at the "myURL" service side?

    Or would following help?
    1.Change the build platform target to x64.
    2.Add following in the service config file?

        <runtime>
            <gcAllowVeryLargeObjects enabled="true" />    
        </runtime>

    Please advise.

    Thanks for your help.



    Friday, September 23, 2016 5:48 PM

Answers

  • Hi,

    HttpResponseMessage is also implementing IDisposable so you should dispose that instance, too.

    (So my suggestion would be to wrap that into a using statement as you did with the client).

    And I would suggest that you check other used classes for IDisposable, too, because this might be a reason for memory problem.

    With kind regards,

    Konrad

    Monday, September 26, 2016 6:05 AM

All replies

  • The thing with OOM exceptions is that usually the place the exception comes from is not the place where the problem comes from. The other code is more likely to be at fault.

    Troubleshooting System.OutOfMemoryExceptions in ASP.NET : Microsoft Support Team's IIS Blog : The Official Microsoft IIS Site

    You get a OOM if you either handle realy large stuff (tables and images in particular).
    Or if you got a reference memory leak, sowhere in the rest of the code.

    How big is the return set of the queries?
    Do you handle BLOBS or even images directly (for processing) in your code?
    Do you have any global, mutable state in your code?
    Any collections you keep adding to, but might forget to remove from?

    WCF belongs to the web technologies. And those are usually designed to not keep any information around between two calls. But if you try to cache something in any way that can quickly backfire.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Friday, September 23, 2016 6:47 PM
  • Maybe the WCF forum is where you need to post?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

    Saturday, September 24, 2016 12:41 AM
  • Hi ANi2000,

    Since your problem is more related to WCF. I will move it to WCF Forum for support.

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 26, 2016 3:17 AM
  • Hi Ani,

    Have you tried suggestion from Christopher?

    Did you develop with WCF Rest Service? Is GetProducts a WCF method or it is a simple function method which is called by WCF method A? I would suggest you try to call it from other place like console application or Fiddler.

    In addition, is CreateHttpClient a custom class object which you implement by HttpClient? A simple demo which could reproduce your issue would be much helpful.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, September 26, 2016 5:48 AM
  • Hi,

    HttpResponseMessage is also implementing IDisposable so you should dispose that instance, too.

    (So my suggestion would be to wrap that into a using statement as you did with the client).

    And I would suggest that you check other used classes for IDisposable, too, because this might be a reason for memory problem.

    With kind regards,

    Konrad

    Monday, September 26, 2016 6:05 AM