title: Web API system out of memory exception
body: <p>I know there are a lot of questions regarding this issue. But I am unable to solve my problem. </p>
  
  <p><strong>API Flow</strong></p>
  
  <ol>
  <li>It accepts two parameters meter serial number and date time. </li>
  <li>After the parameters are passed the API call is made</li>
  <li>The API will search for the sent meter serial number in two databases. </li>
  <li>After the record is fetched it should give the output.</li>
  </ol>
  
  <p><strong>Code</strong></p>
  
  <p>Below is my code </p>
  
  <pre><code>public HttpResponseMessage GetDetails(string msn, DateTime dt)
  {
      try
      {
          var prodDetails = mdcEntitites.tj_xhqd.Where(m =&gt; m.sjsj &gt;= dt)
                              .Select(x =&gt; new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd })
                              .ToList();
  
          var mainDetails = kesc.tj_xhqd.Where(m =&gt; m.sjsj &gt;= dt)
                              .Select(x =&gt; new { MSN = x.zdjh,PingDateTime= x.sjsj,PingValue = x.xhqd })
                              .ToList();
  
          var res = prodDetails.Concat(mainDetails).ToList();
          return Request.CreateResponse(HttpStatusCode.OK, new {details = res });
  
      }
      catch (Exception ex)
      {
          return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
      }
  }
  </code></pre>
  
  <p>In above call, I am accepting a date time. When a meter is dispatched to field staff the date time is marked in the system, so the above date is that date time.</p>
  
  <p>It will search all the records of that serial number after this date time. </p>
  
  <p><strong>Error</strong></p>
  
  <p>Using <code>Postman</code> when I try to run the API with current date time it gives me the following result</p>
  
  <pre><code>{
  "details": [
      {
          "MSN": "002998002523",
          "PingDateTime": "2018-06-21T08:38:12",
          "PingValue": "26"
      },
      {
          "MSN": "002998001286",
          "PingDateTime": "2018-06-21T08:38:13",
          "PingValue": "18"
      },
      .
      .
      .
      .
      .
    ]
  }
  </code></pre>
  
  <p>But when I try to run the API with date time less than current date time it gives me below exception</p>
  
  <blockquote>
    <p>Exception of type 'System.OutOfMemoryException' was thrown.</p>
    
    <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
    
    <p>Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.</p>
  </blockquote>
  
  <p><strong>Source Error:</strong></p>
  
  <blockquote>
    <p>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p>
  </blockquote>
  
  <p><strong>Stack Trace:</strong></p>
  
  <blockquote>
    <p>[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
     System.IO.MemoryStream.set_Capacity(Int32 value) +89
       System.IO.MemoryStream.EnsureCapacity(Int32 value) +90
       System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +326
     Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.ArteryFilter.Write(Byte[] buffer, Int32 offset, Int32 count) +62
    System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +9746340
       System.Web.HttpResponse.FilterOutput() +104
       System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +58
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +71</p>
  </blockquote>
  
  <p>How can I get rid of this issue? </p>
  
  <p>Any help would be highly appreciated.</p>