Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Unable to clear memory of large generic collection

i am putting 2 very large datasets into memory, performing a join to filter out a subset from the first collection and then attempting to destroy the second collection as it uses approximately 600MB of my system's RAM. The problem is that the code below is not working. After the code below runs, a foreach loop runs and takes about 15 mins. During this time the memory does NOT reduce from 600MB+. Am i doing something wrong?

List<APPLES> tmpApples = dataContext.Apples.ToList(); // 100MB
List<ORANGES> tmpOranges = dataContext.Oranges.ToList(); // 600MB

List<APPLES> filteredApples = tmpApples
    .Join(tmpOranges, apples => apples.Id, oranges => oranges.Id, (apples, oranges) => apples).ToList();
tmpOranges.Clear();
tmpOranges = null;
GC.Collect();

Note i re-use tmpApples later so i am not clearing it just now..

like image 614
Grant Avatar asked Dec 21 '22 16:12

Grant


1 Answers

A few things to note:

  • Unless your dataContext can be cleared / garbage collected, that may well be retaining references to a lot of objects
  • Calling Clear() and then setting the variable to null is pointless, if you're really not doing anything else with the list. The GC can tell when you're not using a variable any more, in almost all cases.
  • Presumably you're judging how much memory the process has reserved; I don't think the CLR will actually return memory to the operating system, but the memory which has been freed by garbage collection will be available to further uses within the CLR. (EDIT: As per comments below, it's possible that the CLR frees areas of the Large Object Heap, but I don't know for sure.)
like image 78
Jon Skeet Avatar answered Dec 28 '22 23:12

Jon Skeet