- Home /
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[i]) 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<bool> v;
void Start () {
v = new List<bool>(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!
According to the compiler, that is a private list that is assigned once and never used. Why should it keep it around?
@TreyH Ok. But why does compiler think the list is not used if I use it in Update()? I made a for loop, printed out values of list like v[i] and also tried reassigning a new value to every element in list because I suspected referencing issue too. Setting it to public didn't work either but I haven't tried to access it from other script yet. If it makes difference.
Btw. If I print v.Capacity in Update(), output is "500000" even though profiler shows GC alloc as 488,4$$anonymous$$B in Test.Start()
btw:
500 $$anonymous$$B == 488.28125 $$anonymous$$iB
So it actually allocated a bit more than the 500000 required bytes. A List has some additional data ("Count" as well as a managed reference to the actual array. Also that array itself also has the internal "Length" stored in memory).
Answer by Bunny83 · Nov 15, 2016 at 06:09 PM
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.
@Bunny83 Oh no...lol I'm pretty embarassed right now... So "GC alloc" doesn't mean the amount of collected memory but actually allocated heap memory.
I'm pretty sure someone's been wondering if my question was just written by "a troll" but I've spent like 2 days with this... =)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Find GameObjects with a certain Script [Solved] 1 Answer
print the whole list to screen 1 Answer
Find the differences between two lists 1 Answer
Show and Edit Attributes of my list of structs in EditorWindow. 0 Answers