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 =/.

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.

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.