I'm trying to add a lot of data to a List, but it seems to use a lot more RAM than an array does. I was wondering why that is and if there's a better solution.
This solution with an array takes about 78 MB of RAM. Makes sense since 4 byte * 20000000 ~= 76 MB:
float[] arrayValues = new float[20000000];
for (int i = 0; i < 20000000; i++)
arrayValues[i] = i;
But this solution with a list takes 206 MB (!!):
List<float> listValues = new();
for (int i = 0; i < 20000000; i++)
listValues.Add(i);
How can that be? It's basically doing the same thing - saving 20000000 float values. Where is that additional 128 MB coming from? And is there a better way that doesn't produce so much overhead?