Is it worth it?

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?

There is no explicit call to free() if that is what you are looking for.

Memory will eventually be reused, however the garbage collection happens deep within the bowls of mono itself you and have little control over the process. This can cause slight but noticeable hiccups in some instances, when garbage collection runs at an inopportune moment in your game. Profiler helps there.

You have though figured though out what most people do. Cache the instances of your planets and recycle them instead of destroying them. Not so much for your 10m of planet data, but often used in situations where you are constantly creating and destroying large numbers of objects, projectiles or whatnot.

Actually Unity objects and Mono objects are separate. Currently, I think Unity objects are cleaned up when loading a new level. Unity iPhone has Application.GarbageCollectUnusedAssets, presumably because of having a much tighter restriction on memory, so you might want to do that manually at times. I’d expect Unity will get that too in 3.0.

–Eric