I remember a couple past discussion threads about memory managemetn in Unity and garbage collection. If I understood the gist of those corerctly, destroying a game object does not free up memory. Is this correct? If I create 10 objects that each eat up 1MB of memory, and then I destroy those objects, I don’t get that 10MB of memry back?
The reason I ask is this. I am building a galaxy. I know. Awe inspiring. Anyway, because it would be silly to have all of the stars and their planets and their moons and asteroid belts and comets and spaceships all doing their things at once, I am basically just physically representing, at any given time, one star system. To do this, I have toyed with two methods. The first is to have a generic game object representation of a star system that takes the data for whichever star system I want to represent and modifies itself to match.
For example, I have a star prefab and 10 planet prefabs. The are all of scale 1, and have not texture or orbit data, or anything else. When I load a star system in, the data for the system is applied to the objects so they take the physical form of the system. The star scales up and its light intensity and color are adjusted. Textures are applied to everything, and the correct number of planets are set to be visible, and they are adjusted accordingly as well. Everything is put into motion and, voila, a living, breathing star system. When the next star system is set to be loaded, these same objects are used and their attributes are adjusted to correspond to the new system data.
My thinking for this strategy is that, if memory isn’t freed by destroying gameobjects, then why not just use the same ones over and over again? That would certainly be better than destroying and creating whenever a star system needs, or no longer needs, to be physically represented, right?
The other method is just that, though. If memory is freed whenever objects are destroyed, then I could destroy my star systems when they aren’t needed, and create new objects when they are. This would mean performance gains in memory management.
So, which is it? Does memory get freed or not when gameobjects are destroyed?