2

I have a rather simple method within a data model class that does nothing more than insert a single row and save the changes. It correctly processes about 227 records [out of 408] and then returns an OutofMemoryException. This is occurring even though we're only processing single row entries and saving the changes. Any ideas how to resolve this situation?

protected ADMIN_DB_Entities _AdminEntities = new ADMIN_DB_Entities();

public void InsertBase64Payload(string sBase64PayloadValue, string sSourceValue, Guid gSourcePrimaryKey, DateTime dDateProcessed)
{
    Base64Payload newBase64PayLoadEntry = new Base64Payload();
    newBase64PayLoadEntry.BASE64_VALUE = sBase64PayloadValue;
    newBase64PayLoadEntry.SOURCE_TABLE = sSourceValue;
    newBase64PayLoadEntry.SOURCE_PRIMARY_KEY = gSourcePrimaryKey;
    newBase64PayLoadEntry.DATE_PROCESSED = dDateProcessed;
}
try
{
  _AdminEntities.Base64Payload.Add(newBase64PayLoadEntry);
  _AdminEntities.SaveChanges();
}
catch (Exception ex)
{
    ConsoleAppLog.WriteLog(<Error Message here.>)
}
  • Per record that loads in do you see the memory usage going up? You are apparently working with very large Base64 values and they eventually take up all your available memory. – Bailey Miller Sep 07 '17 at 16:09
  • Either you need to do some garbage collection manually to keep junk memory low, or look into the settings of EF to see how to keep things from enumerating to the point of OOM exceptions. – Bailey Miller Sep 07 '17 at 16:12
  • https://stackoverflow.com/questions/15941471/entity-framework-4-out-of-memory-on-savechanges – Bailey Miller Sep 07 '17 at 16:13
  • I do see the memory usage going up. Console application is being executed directly on DB server. Server is already operating close to the max at steady state. Base64 data is large. Other methods looking identical and with different data doesn't create the same problem. – Anonymous3521 Sep 07 '17 at 16:14

1 Answers1

2

I presume you are working with very large base64 "payloads".

EntityFramework's DbContext saves entity's state inside memory. So, even after you save changes into database, those values will be inside process memory. DbContext implements IDisposable interface, so in such scenarios it's better to dispose context after you saved desired data into database:

using (var entites = = new ADMIN_DB_Entities())
{
   try
   {
      entities.Base64Payload.Add(newBase64PayLoadEntry);
      entities.SaveChanges();
   }
   catch (Exception ex)
   {
      ConsoleAppLog.WriteLog(ex.Message);
   }
}

Note: Keep in mind that there is mechanism in which you can de/attach particular entity from internal database context's tracking, so if needed, you can also work with the single DbContext instance.

Darjan Bogdan
  • 3,780
  • 1
  • 22
  • 31