none
Why I cannot free memory in EF 4.1? RRS feed

  • Question

  • Hi, I need to know how I can free the memory after I am done with Entity Collection short of quiting application. During the application runtime I am loading thousands of records and I would like to do it in batches and free the memory after. I cannot find any way of doing it or mentions of any way of doing it. Is it possible with EF?

    Plus I would like to know why the Dispose (using clause) doesn't remove all entries, but make it looks like they are static in some way. Looks like a pretty bad bug to me. Here is an example:

        internal class Program
        {
            private static void Main()
            {
                using (var context = new MetaDataEntities())
                {
                    for (int i = 0; i < 500000; i++)
                    {
                        var table1 = new Table_1 {Name = "Dmitriy", Zip = "33328"};
                        context.AddToTable_1(table1);
                   }
                }
     
                var newContext = new MetaDataEntities();
                var cnt = newContext.Table_1.Count();
            }
        }

    The count after I am creating new context is still 500000 records. The entity model is supersimple: id-identity and two fields Name and Zip so I am not even going to post it.

    Anybody had the same problems and know how to fix them? Would really like an answer and not a workaround with suggestion on how to use something else entirely.


    • Edited by Dmitriy Tuesday, January 24, 2012 8:22 PM
    Tuesday, January 24, 2012 8:21 PM

Answers

  • Hi Dmitriy;

    To your statement, "but Collect does nothing. It still takes an idle time of around 5 minutes to release memory and if I start new batch immediately memory footprint just grows.", I am not sure what you mean or how you determined that GC.Collect() is doing nothing. Also when you say that, "I start new batch immediately", does that mean that you start a new instance of the program while the other is still running?

    To your question, "Is there any way to stop the process while memory clears?", What process? It would be helpful to see the code you are working with as well.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, January 24, 2012 9:56 PM
  • Hi,

    Also keep in mind that a GC is basically based on the idea that there is no point in reclaiming immediately some memory if you have still some available memory.

    I would suggest a C# groups as this is more related to how memory management in C# works rather than really EF.

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, January 25, 2012 9:46 AM

All replies

  • Hi Dmitriy;

    After leaving the using statement the context is disposed. Now they may linger around in memory until the system runs the garbage collection. Garbage collection will run if low on memory but if you need to free up memory you can run garbage collection outside of the using statement by issuing the command GC.Collect( );, but please not that there may be a performance hit in doing this.

    When you do this outside the using statement :

    var newContext = new MetaDataEntities();
    var cnt = newContext.Table_1.Count();

    The value returned in cnt is the number of rows currently in the table Table_1 in the database.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, January 24, 2012 9:32 PM
  • Sorry I forgot that I ran save. So yes looks like entries are cleared, but Collect does nothing. It still takes an idle time of around 5 minutes to release memory and if I start new batch immediately memory footprint just grows.

    Is there any way to stop the process while memory clears?

    Tuesday, January 24, 2012 9:38 PM
  • Hi Dmitriy;

    To your statement, "but Collect does nothing. It still takes an idle time of around 5 minutes to release memory and if I start new batch immediately memory footprint just grows.", I am not sure what you mean or how you determined that GC.Collect() is doing nothing. Also when you say that, "I start new batch immediately", does that mean that you start a new instance of the program while the other is still running?

    To your question, "Is there any way to stop the process while memory clears?", What process? It would be helpful to see the code you are working with as well.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, January 24, 2012 9:56 PM
  • Hi,

    Also keep in mind that a GC is basically based on the idea that there is no point in reclaiming immediately some memory if you have still some available memory.

    I would suggest a C# groups as this is more related to how memory management in C# works rather than really EF.

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, January 25, 2012 9:46 AM