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!