While preparing large amount (several thousand rows) of data and insert them to database through Entity Framework, creating the Entity objects or the in-memory object-graphs doesn’t take up that much memory, but when the SaveChanges() method is called, over the duration it continues to consume noticeable amount of memory until the method returns.
What exactly happens under-the-hood during the period of the SaveChanges() call that causes this memory consumption?
It depends on the depth of your object graph, the deeper the graph the more memory is going to be consumed. Generally the more costly operations that happen after you call
SaveChanges
are the following:The cost of actually executing the queries is relatively low. See here for more info.
In general, it is recommended to use special strategies for bulk inserting with EF. See here.