I noticed this while using SQLite with System.Data.SQLite while writing an application that queried the database very often.
The working memory set for the application kept growing, so i started .NET Memory Profiler and checked. I noticed that literally ten-thousands of SQLiteCommand objects are noted as undisposed, if i let the application run as much as 5 minutes. And the number increased fast.
I checked the call-stack and all of them were created from either SQLiteCommand.Clone() which is called from System.Data.Entity!System.Data.Common.DbCommandDefinition.CreateCommand() or SQLiteFactory.CreateCommand() which is called from System.Data.Entity!System.Data.EntityClient.EntityCommand.get_CommandTimeout()
Since the two SQLite methods aren't the problem it has to be the EF, so i used the northwind database to perform a quick test.
This:
while (true)
{
using (northwindEFEntities db = new northwindEFEntities())
{
db.Customers.MergeOption = System.Data.Objects.MergeOption.NoTracking;
int count = db.Customers
.Count()
;
count++;
}
}
will create a small but steadily growing leak, while this:
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.DataSource = @".\northwindEF.db";
string sql = @"SELECT count(*) FROM Customers";
while (true)
{
using (SQLiteConnection cnn = new SQLiteConnection(builder.ConnectionString))
{
cnn.Open();
using (SQLiteCommand com = new SQLiteCommand(sql, cnn))
{
int count = Convert.ToInt32(com.ExecuteScalar());
count++;
}
}
}
provides no leak at all.
Seems like the EF doesn't disposes it's commands correctly.