9
votes

I am trying to save hundreds of thousands of records using Entity framework. After saving few hundreds of thousands of records I get following error:

:System.OutOfMemoryException

My code

  foreach (BibContent objbibcontents in lstBibContent)
        {
            db.BibContents.AddObject(objbibcontents);
            c = c + 1;
            if (c == 1000)
            {
                db.SaveChanges();
                c = 0;
            }
        }

I noticed after saving 1000 records my db is not overriding another 1000 records. it is adding them into my dbcontext.

I am creating a new instance after 1000 records but my db still has the previous object's data. See my code

   foreach (var objbibcontents in lstBibContent)
            {
                vibrantEntities db1 = new vibrantEntities(szConStr);
                lstBibCon.Add(objbibcontents);
                // db.BibContents.AddObject(objbibcontents);
                c = c + 1;
                if (c == 1000)
                {
                    foreach (BibContent bibobject in lstBibCon)
                    {
                        db1.BibContents.AddObject(bibobject);
                    }
                    lstBibCon.Clear();
                    db1.SaveChanges();
                    c = 0;
                    flag = 1;
                }
            }
2

2 Answers

13
votes

How many objects are you going to save and how big is single object? DbContext holds references to all objects you added with AddObject call. Calling SaveChanges does not purge its internal data structures so if you call your code for 1M objects you will have 1M object in memory and they will be fully alive because their root object for GC will be the context instance which is still in scope of your running code.

If you want to avoid the memory issue you should use a new context instance for every 1000 records (or even every record). The only difference between running SaveChanges for 1000 records and for single record is the automatically involved transaction.

4
votes

I search on Web and Finally i found good solution for my problem.

Fastest Way of Inserting in Entity Framework