-1

While trying to add huge lists into a list of list of int, am getting an out of memory exception. Posting a reproducible snippet below to illustrate the issue:

List<long> Arr = new List<long>();
List<List<long>> lstArr = new List<List<long>>();

for (long i = 0; i < 20000; i++)
{
    for (int k = 0; k < 20000; k++)
    {
        Arr.Add(k);
    }

    // Below is the line that generates the exception
    lstArr.Add(Arr.ToList());
    Arr.Clear();
}

Console.ReadLine();

Memory exception appears

How can we rectify this problem? Please note that we should be ideally using List<List<int>>.I completely understand that this is a heap memory allocation issue. So am trying for is a code workaround /alternate to fix this issue. thanks again!

MR K
  • 113
  • 1
  • 1
  • 12
  • 1
    Possible duplicate of [What's the max items in a List?](https://stackoverflow.com/questions/2009217/whats-the-max-items-in-a-listt) – GSerg Dec 29 '19 at 14:05
  • Try using arrays, with predefined length, check this script, ` var Arr = new long[20000]; var lstArr = new long[20000]; for (long i = 0; i < 20000; i++) { for (int k = 0; k < 20000; k++) { Arr[k] = k; } lstArr[i] = i; Console.WriteLine(i); } ` – Hassan Alhaj Dec 29 '19 at 14:18
  • If you cannot set `gcAllowVeryLargeObjects`, then another workaround is to have several lists, each under the limit. – GSerg Dec 29 '19 at 14:24

1 Answers1

0

Please try to set gcAllowVeryLargeObjects in your app.config , see this link. There is a heap memory size limit and you're probably exceeding it.

NickDelta
  • 3,697
  • 3
  • 15
  • 25
  • Yes i agree that its a heap memory size limit , but unfortunately i dont have access to the config file . Workaround needs to be in the code itself – MR K Dec 29 '19 at 14:17
  • @MRK An another option is to find a disk-based solution (like databases). When we want to process large amounts of data, we don't store everything in memory. Instead we make queries to retrive portions of data from the disk and process them. In a production application, your code would be a huge memory leak. – NickDelta Dec 29 '19 at 14:24