I'm experiencing OutOfMemory exceptions in my application, when fetching from the database. It is a C# .Net application using Linq2Sql.
I have tried using GC.GetTotalMemory() to see how much memory is taken up before and after the call to the database. This gives a nice although not quite accurate picture of what is going on. When I look in the Windows Task Manager I can see that the Peak Working Set is not smaller when fetching the data in a paged manner using the following code:
public static void PreloadPaged()
{
int NoPoints = PointRepository.Count();
int pagesize = 50000;
int fetchedRows = 0;
while (fetchedRows < NoPoints)
{
PreloadPointEntity.Points.AddRange(PointRepository.ReadPaged(pagesize, fetchedRows));
PointRepository.ReadPointCollections();
PreloadPointEntity.PointCollections.Count());
fetchedRows += pagesize;
}
}
private static List<PointEntity> ReadPaged(int pagesize, int fetchedRows)
{
DataModel dataContext = InstantiateDataModel();
var Points = (from p in dataContext.PointDatas
select p.ToEntity());
return Points.Skip(fetchedRows).Take(pagesize).ToList();
}
I guess it's the Linq2Sql code that is using up the memory and not reusing it or freeing it afterwards, but what can I do to get the memory foot print down?
I have observed that it uses 10 times as much memory to fetch the data as it does to store them in my list of enties. I have considered invoking the garbage collector, but I would rather avoid it.
50000rows to be retreived at once?