Memory Spike

Hello.

In our game, we have several different menu scenes and one big gameplay scene. In the menu scenes, memory consumption remains, according to Instruments, around 25MB. But when we move from a menu scene to the gameplay scene, the memory consumption climbs to 45MB during the loading of the scene, then immediately drops back down to 37 when the loading is complete. The problem is that the 45MB spike causes some devices to run out of memory and crash.

So my question is: Is it normal for Unity to allocate a lot of memory during the loading of a scene, even though the scene doesn’t need that much memory when running? My other theory was that some things from the menu scenes remain in memory during the loading, but when I added a very light scene between the menu and gameplay scenes, the problem didn’t go away. (I even called System.GC.Collect() in that lightweight scene.)

There doesn’t seem to be such a memory spike with all scenes, just this one…

One thing that potentially is related to this is OnGUI

During the 1.0.x times at very least, another thing that was commonly done was injecting an empty scene between scene switches that does nothing else than load the real scene. This ensured a fully cleaned asset side and thus leading to smaller ram requirements.
What you potentially see is the unload of the old data in parallel with loading the new data.

by the way: using System.GC does not help with unity. Unity keeps internal references to everything.
the purge function from the Application class would be used for this but is not required as loadlevel automatically calls it

I heard (read?) a theory on IRC that textures only show in the Instruments memory allocation report the first time they’re drawn and then vanish. My further investigations support this theory - the more textures I load, the more spikes there are.