4

I'm profiling memory use with ANTS Memory Profiler 7.0 and noticed that unmanaged memory use is ~193MB (~62%) for a console application that does little more than populate some DTOs from 10 million or so records.

The help text for unmanaged memory says:

The memory is assigned to the parts of the application that aren't running as pure .NET code. This includes the common language runtime itself, graphics buffers and any unmanaged data accessed through P/Invoke or COM+

Why might this figure be so high?

Kofi Sarfo
  • 3,310
  • 6
  • 23
  • 24
  • 3
    What library are you using for data access? – ChaosPandion Sep 03 '12 at 13:13
  • can you show the code that populates the DTO's? – IvoTops Sep 03 '12 at 13:31
  • Sorry. I should have added that data access is via Entity Framework and, yep, the contxt is created with a using block so will be disposed. I thought of that but no cigar. I've since tried running ANTS against a simple console app that only iterates to 100,000 and populates an integer array. No data access. No graphics. No P/Invoke. Still the memory profiler shows unmanaged memory at 61% – Kofi Sarfo Sep 03 '12 at 17:09

1 Answers1

6

You will inevitable use unmanaged code when accessing a database. The interface to the engine is always code that's been around for a long time, predating .NET and wrapped by managed classes that provide the interop. True for, say, SQL Server and any provider that piggy-backs onto OleDb or ODBC.

These managed classes will always implement IDisposable so you can release the resources consumed by the native provider early. Forgetting to do so is very common and rarely noticed. Other than seeing the process running "heavy", seemingly consuming a lot of handles and unmanaged memory for no good reason. This will be especially the case when the garbage collector does not run frequently enough, something you can see with Perfmon.exe. So beyond not using Dispose, part of the problem can be that you don't do enough work with these DTO objects yet to get enough GC churn.

Review your code and ensure you use Dispose() and the using statement where required.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536