Why is GC collecting list with capacity?

Hi. I have a scene with one object that uses one script called “Test”. Problem is that the following code creates 488,4 KB of garbage for the GC if I construct the list with capacity. Yet, I need to set capacity somewhere to avoid resizing of the list if I add stuff to it. I’ve tried to find reasons for GC alloc like by making a bool member and setting it values of the list (like in for loop, member = v*) in Test.Update() to give a reason for v to exist and not be deallocated. Deep profiling shows that GC collection happens in Start() already, also without the for loop.*
Here’s the code:
using UnityEngine;
using System.Collections.Generic;

public class Test : MonoBehaviour {
List v;

void Start () {
v = new List(500000);

for (int i = 0; i < v.Capacity; i++)
{
v.Add(false);
}
}

void Update () {

}
}
In my game project I want to keep a list to store data but fill it later after I know what the data is. So the list is empty at first and I’m initializing (and giving capacity) for members in Awake().
So, why does list “v” cause GC alloc and how do I avoid that but still could set capacity?
Thanks!

I think you’re confusing allocation with collection of garbage. As seen from the GC every bit of memory is potential garbage. Whenever you allocate memory the GC will keep track of everything. Of course when you execute this line:

v = new List<bool>(500000);

You will allocate about 500k of memory. However that memory is not collected as it’s still in use. When garbage memory is collected it’s always done from the GC-thread and never from the main thread.