Disposing DataServiceContext?
Asked Answered
O

2

7

I have created an application that reads CSV files, creates a DataServiceContext and mass inserts the data to my remote OData API server.

However 5 minutes after running an import of 30,000 records, I found that the application was still using 750MB of memory!

Is there anything I should do to reduce the memory usage? Or at least make it garbage collect earlier? It doesn't seem to implement IDisposable, and my google-fu has failed me. Thanks.

Ophir answered 18/2, 2016 at 15:14 Comment(0)
B
5

I had a similar issue with the Microsoft.OData.Client.DataServiceContext class that looks similar to the System.Data.Services.Client.DataServiceContext.

What was happening was that the DataServiceContext has an EntityTracker that is used to track any changes done to the entities being iterated. After some looking around I saw that DataServiceContext has a MergeOption property. To solve your problem set it to NoTracking like so:

dsc.MergeOption = MergeOption.NoTracking;

This should be done once before starting the enumeration, be it in the constructor or anywhere before the loop.

Boatsman answered 30/8, 2016 at 8:20 Comment(1)
But if you do this, you will not be able to use the LoadProperty method without it throwing an exceptionThermel
O
4

I've been using this after processing to clear the context, if it helps anyone.

public static void ClearChanges(this DataServiceContext context)
{
    foreach (var entity in context.Entities.ToList())
    {
        context.Detach(entity.Entity);
    }

    foreach (var link in context.Links.ToList())
    {
        context.DetachLink(link.Source, link.SourceProperty, link.Target);
    }
}
Ophir answered 30/8, 2016 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.