How do you concatenate huge lists without doubling memory?
Consider the following snippet:
Console.WriteLine($"Initial memory size: {Process.GetCurrentProcess().WorkingSet64 /1024 /1024} MB");
int[] a = Enumerable.Range(0, 1000 * 1024 * 1024 / 4).ToArray();
int[] b = Enumerable.Range(0, 1000 * 1024 * 1024 / 4).ToArray();
Console.WriteLine($"Memory size after lists initialization: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB");
List<int> concat = new List<int>();
concat.AddRange(a.Skip(500 * 1024 * 1024 / 4));
concat.AddRange(b.Skip(500 * 1024 * 1024 / 4));
Console.WriteLine($"Memory size after lists concatenation: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB");
The output is:
Initial memory size: 12 MB
Memory size after lists initialization: 2014 MB
Memory size after lists concatenation: 4039 MB
I would like to keep memory usage to 2014 MB after concatenation, without modifying a and b.