- Home /
Garbage collection
Hey guys, this is a real nooby question but then I am essentially a nooby programmer. With respects to garbage collection within unity, as I currently understand it, unity will handle any garbage collection after a new scene has been loaded. That is to say once you load a scene all memory use from the previous scene will be freed up. But, if I am making frequent use of large lists etc within the current scene, do I need to make sure I go through the code using list.Clear() after I no longer need it?
I'm pretty sure this is fundamental basic programming knowledge but our tutors didn't even touch on garbage collection =/.
Answer by kaushik-d · Oct 19, 2013 at 05:53 PM
If your lists contain GameObjects that are de-activated and won't be needed further in the game, you should destroy those GameObjects (using Destroy()) before clearing your list. If they are references to active objects, clearing these lists wouldn't really help in terms of memory - the instances of these objects in your list will be probably much bigger than the references to them.
Answer by Eric5h5 · Oct 19, 2013 at 06:16 PM
Garbage collection only works on Mono variables (ints, floats, etc.), which includes lists and arrays. Unity objects (GameObject, Texture2D, etc.) are not part of Mono and are not garbage collected. Loading a new level will destroy all Unity objects and call Resources.UnloadUnusedAssets, which you can also call manually if needed.
To add to Eric's response, be aware of the fact that large $$anonymous$$ono arrays are not garbage collected: http://answers.unity3d.com/questions/225460/big-arrays-are-never-garbage-collected-why.html
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
garbage collection fix 0 Answers
Collections don't work! 1 Answer