1

In my project all the data fetched from database is compressed using GzipCompression. I am facing outof memory exception only when I am fetching data from the table with code ="XYE01" which have 315 records.

I tried GC.Collect() and GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; before compressing data still I am facing the issue

[AttributeUsage(AttributeTargets.Method)]
public class GZipCompressionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        if (actionExecutedContext.Response != null)
        {
            try
            {
                var content = actionExecutedContext.Response.Content;
                var bytes = content != null ? content.ReadAsByteArrayAsync().Result : null;
                var zlibbedContent = bytes != null ? GZipCompressionHelper.Compression(bytes) : new byte[0];
                actionExecutedContext.Response.Content = new ByteArrayContent(zlibbedContent);
                actionExecutedContext.Response.Content.Headers.Remove("Content-Type");
                actionExecutedContext.Response.Content.Headers.Add("Content-Encoding", "gzip");
                actionExecutedContext.Response.Content.Headers.Add("Content-Type", "application/json");
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, string.Format("Cannot compress on query: {0}.", actionExecutedContext.Request.RequestUri.PathAndQuery), ex);
            }
        }

        base.OnActionExecuted(actionExecutedContext);
    }
}

  #region  publicMethods
    // GET api/RequiredStartingValue/GetRequiredStartingValues?code=
    [GZipCompression]
    [HttpGet, CacheOutput(NoCache = true)]
    [UnitOfWorkAction(ReadUncommitted = true)]
    public IHttpActionResult GetRequiredStartingValues(string code)
    {
        List<StartingValue> requiredStartingValues = _startingValuesRepository.GetRequiredStartingValues(customerCode);
        return Ok(requiredStartingValues );
    }
    #endregion

How to increase the memory? Other tables where 900 rows coming is also working fine but with 315 rows it is throwing out of memory exception I am not able to understand.

Please provide me some suggestions as I am new to WebAPI and Database fetching I am unable to understand the root cause.

Thanks, Nagasree.

NAGASREE
  • 342
  • 4
  • 15
  • Could you add stacktrace from your exception? Can you modify query to return less rows? Just 2 for example - is this error still occuring? – Leszek P Jun 23 '21 at 11:30
  • is this old WebAPI 2.2 or Net core? If Net core then have a look https://stackoverflow.com/questions/3802107/how-to-gzip-content-in-asp-net-mvc – Leszek P Jun 23 '21 at 11:33
  • @LeszekP With less no of rows it is not throwing exception – NAGASREE Jun 23 '21 at 11:34
  • It is not Net Core. It is Angualr Js + .Net framework 4.5.1 – NAGASREE Jun 23 '21 at 11:36
  • Check Task Manager and see if you are using all the memory? The only way of solving is to add more memory, close applications that are using memory, or make sure you are using swap space where the hard drive will be used when you exceed memory (will slow down app). – jdweng Jun 23 '21 at 12:47
  • @NAGASREE, you may already know this, but the 315 records, from the table you are referring to, can still take up more memory than 900 records from say a lookup table, especially if the table in question (with the 315 rows) stores large binary objects in something like a BLOB format. – fradsham Jun 23 '21 at 16:43

0 Answers0