in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

EF doesn't dispose SQLiteCommands correctly

Last post 02-26-2010 12:00 AM by Cuong. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 02-24-2010 1:21 PM

    EF doesn't dispose SQLiteCommands correctly

    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.

  • 02-26-2010 12:00 AM In reply to

    • Cuong
    • Top 50 Contributor
    • Joined on 02-02-2009
    • Posts 19

    Re: EF doesn't dispose SQLiteCommands correctly

    Hi Schuck, I have not tried to run your test yet. But think you are right, it seem there is a memory issue with SQLite.Net EF.

    In my Silverlight client/server application, I am using SQLite.net EF in the server side. I found that the memory increases very quickly and it does not decrease as expecting. I always have to reset IIS 2 times/1 day to clear the memory. My application's old version used Access db and I did not see this problem. My other application that only uses SQLite.net without EF also works fine, no memory leak problem.


     

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems