Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Issue: [Investigate EF6] EF6 RC1: Severe memory leak when inserting many entities [doesn't repro with EF5] [1605]

$
0
0
I've recently started writing a new component, and chose EF6 RC1 Code-first. The model consists of only two classes composed of primitive types. No relationships, no complex types, nothing fancy except that they both inherit form the same abstract base class, but I do not use EF's inheritance features (such a TPH/TPT/TPC), but instead simply expose two `DbSet<>`s, one per concrete type.

Trying to intensively insert around 780k records caused the process' memory to balloon instantly, and eventually reach an OutOfMemoryException when hitting the 32-bit process memory limit in only two minutes. Even when loading a limited amount of records, which completes successfully, the memory is never released. I loaded the process until it reached around 700 MB (and waited a bit for the garbage collector to calm down), then analyzed the memory using WinDbg, and these are the 20 top offenders:

```
Statistics:
MT Count TotalSize Class Name
680fcf54 538 6358990 System.Byte[]
680fc480 872240 10466880 System.Int32
06c0a0b8 749728 11995648 System.Data.Entity.Core.Common.CommandTrees.DbSetClause
06c63a64 750085 12001360 System.Data.Entity.Core.Mapping.Update.Internal.PropagatorResult+SimpleValue
6895d118 55773 17601100 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, mscorlib],[System.Object, mscorlib]][]
680e0184 46858 17806040 System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Object, mscorlib]][]
06c0757c 749730 17993520 System.Data.Entity.Core.Common.CommandTrees.DbConstantExpression
06c08988 796672 19120128 System.Data.Entity.Core.Common.CommandTrees.DbPropertyExpression
05a023c8 1687471 20249652 System.Lazy`1+Boxed[[System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Data.Entity.Core.Metadata.Edm.MetadataCollection`1+OrderedIndex[[System.Data.Entity.Core.Metadata.Edm.Facet, EntityFramework]], EntityFramework]], mscorlib]]
05a189d8 1835088 22021056 System.Data.Entity.Core.Metadata.Edm.MetadataItem+AnnotationCollection
680fb060 1838275 22059300 System.Object
680fc448 206028 22760400 System.Int32[]
05a01590 1687471 26999536 System.Data.Entity.Core.Metadata.Edm.MetadataCollection`1+CollectionData[[System.Data.Entity.Core.Metadata.Edm.Facet, EntityFramework]]
05a00fa8 1687471 26999536 System.Data.Entity.Core.Metadata.Edm.MetadataCollection`1[[System.Data.Entity.Core.Metadata.Edm.Facet, EntityFramework]]
05a00a58 1687471 26999536 System.Data.Entity.Core.Metadata.Edm.ReadOnlyMetadataCollection`1[[System.Data.Entity.Core.Metadata.Edm.Facet, EntityFramework]]
05a01a90 1687471 33749420 System.Lazy`1[[System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Data.Entity.Core.Metadata.Edm.MetadataCollection`1+OrderedIndex[[System.Data.Entity.Core.Metadata.Edm.Facet, EntityFramework]], EntityFramework]], mscorlib]]
05a01b0c 1687471 40499304 System.Collections.Generic.List`1[[System.Data.Entity.Core.Metadata.Edm.Facet, EntityFramework]]
680facc0 909939 46550944 System.String
680aab98 1955891 58467492 System.Object[]
05a16d18 1688019 74272836 System.Data.Entity.Core.Metadata.Edm.TypeUsage
```

From tallying up the numbers of all the object sizes that are under "System.Data.Entity", you reach almost 300 MB, which is coming close to half the memory the process is consuming! And considering I'm loading only 65 MB of data which results in 75 MB SQL Server database, with only 2 tables that combined have only 37 columns, that's absurd. And of course this leak continues to even greater sizes if I let it, but then the memory dump is very large and WinDbg takes a long time to analyze it since it is single-threaded.

The most definite proof I have is that when I switched to EF5 (by removing the EF6 RC1 NuGet package and installing the EF5 NuGet package), all memory issues were gone. I had almost no code changes to make, except the lack of `DbSet<>.AddRange()` which was converted to a `foreach (...) DbSet<>.Add()`, and the `<providers>` configuration element that had to be removed. I used EF5 as a drop-in replacement, and ran the same job again, but this time the memory consumption of the process never climbed above 100 MB and it completed successfully. This is a serious regression in EF6 RC1 as compared with EF5.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>