System.OutOfMemoryException during NUnit tests with localdb

♀尐吖头ヾ 提交于 2019-12-24 07:46:33

问题


While debugging some NUnit tests we found a memory leak when inserting rows into a localdb via a script file. This has started leading to System.OutOfMemoryException which stops us from running unit tests during development.

The code looks like this:

public static void InsertFromScriptFile (string conString, string dbName, string filePath)
{
   using (SqlConnection conn = new SqlConnection(string.Format(conString, dbName)))
   {
      string script = File.ReadAllText(filePath);

      using (SqlCommand cmd = new SqlCommand(script, conn))
      {
         try
         {
              conn.Open()
              cmd.ExecuteNonQuery();
         }
         catch (Exception ex)
         {
            Debug.WriteLine(ex);
            throw ex;
         }
         finally
         {
            conn.Close();
         }
      }
   }
}

When stepping through the code I get these heap values:

Heap memory step-through

Where:

1) right after entering the inner try/catch
2) is after conn.open
3) is after cmd.ExecuteNonQuery
4) is after the conn.Close in the finally block

In our query we insert a couple of rows into the database. After we started inserting documents as base64 strings we started getting these errors.

When inspecting the objects in the heap after about 10 tests it seems as it's the TdsParserStateObject that's growing, as well as references to our document objects.

TdsParserStateObject

After each TestFixture we drop the localdb and create a new one. We expected the memory to be reclaimed by gc at some point but it keeps growing. Does anyone have any idea why the memory isn't reclaimed?


回答1:


Found a workaround. We tracked the memory being allocated to the localdb instances we created for every test we needed a seed for. These instances were named after the test cases. When we renamed all of them to the same name and ran the tests sequentially (so the tests didn't interfere with each other) memory was only allocated for a single instance.



来源:https://stackoverflow.com/questions/42761087/system-outofmemoryexception-during-nunit-tests-with-localdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!