0

I have a .NET Core 2.2 web API app. It returns rather large chunks of JSON data for a web app that is mostly disconnected from the server.

Throughout the application I return System.Data.DataTable objects from the controllers. A request might be handled like so:

[HttpGet]
public ActionResult<Dictionary<string, object>> Orders(string stuff, DateTime? start, DateTime? end)
{
  var ret = new Dictionary<string, object>();
  var pending_data = new DataTable();

  using (SqlCommand cmd = new SqlCommand("select stuff from stuff", connection)) {
    using (SqlDataReader rdr = cmd.ExecuteReader()) {
      pending_data.Load(rdr);
    }
  }
  ret.Add("pending_orders", pending_data);
  return Ok(ret);
}

Will returning DataTables like this create resource leakage? I'm testing right now and the memory usage seems higher than I anticipated. I may have to write some test code that manually calls the GC to see what the baseline is after each request.

Ian
  • 4,169
  • 3
  • 37
  • 62
  • 1
    "Write some code that manually calls the GC" - [consider not doing that](https://stackoverflow.com/questions/118633/whats-so-wrong-about-using-gc-collect) – Caius Jard Jan 27 '20 at 19:49
  • https://stackoverflow.com/questions/913228/should-i-dispose-dataset-and-datatable –  Jan 27 '20 at 19:50
  • Have you done any profiling? – Muhammad Hannan Jan 27 '20 at 19:50
  • Or you could use dapper, fill some POCO lists with a similar number of lines of code and do away with your worry/check whether it's actually datatable that causes the "problem" you "anticipate" you're seeing – Caius Jard Jan 27 '20 at 19:51
  • DataSet and classes derived from it are not finalized by the GC, because the finalizer has been surpressed in `DataSet` – NotTheBatman Jan 27 '20 at 19:53
  • @CaiusJard - Test code, not production code. I know not to do that for deployment. – Ian Jan 27 '20 at 19:58
  • Last I remembered, data tables always had leaks in them, however, since you're using this with .Net-Core they may have fixed those issues. – johnny 5 Jan 27 '20 at 20:12

1 Answers1

0

After much testing, it does not appear that there is a noticeable leak. Excessive amounts of memory were allocated due to a bug, but none of the memory appeared to be leaking after testing multiple parallel connections to the server. In fact, many parallel connections appeared to make the GC run more frequently and the memory usage decreased overall.

Ian
  • 4,169
  • 3
  • 37
  • 62