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();
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!