Memory does not flush when loading a new scene

After several days of debugging and trial and error, I’m stuck and need some help.

Here is the case:
I’m developing a game for iPhone using Unity iPhone Basic 1.7.0f1 (38208). The game is pretty much done, but I do have one major problem left to fix. Otherwise the game will be rejected, that’s for sure. The game consists of 4 scenes: an intro scene, main menu scene, loading scene and the game scene. The game runs smoothly most of time on my 3Gs but I do experience ‘hiccups’ now and then. Nothing seriously and it haven’t crashed yet, but the results from Apple’s Instruments worries me. In this thread I’ve tried to gather as much data about the game as possible, hoping you guys can point me in the right direction. What I basically don’t understand is why the memory does not get flushed when shifting scenes.

And now to all the data, thank you for your help.

When main menu is the first scene the reel mem shows:
30MB

When game scene is the first scene
38MB

However when I start the main menu as the first scene and go to the game scene the memory usage goes to 56 MB and the game starts to run with an unstable frame rate, it isn’t crashing though.

Using instruments after going back and forth between the main menu and the game scene 279 leaks (8 KB) leaks were detected.

Using the detect leaks script I get following results:

When starting in main menu:
4524
Textures 191
AudioClips 29
Meshes 187
Materials 32
GameObjects 319
Components 1271

When starting the game scene only:
All 4708
Textures 191
AudioClips 29
Meshes 242
Materials 32
GameObjects 319
Components 1390

When going from main menu to game scene:
All 4742
Textures 191
AudioClips 29
Meshes 244
Materials 33
GameObjects 324
Components 1405

When shifting between main menu and game scene 3 times:
All 4757
Textures 191
AudioClips 29
Meshes 245
Materials 33
GameObjects 324
Components 1407

My game includes:
11 textures
14 materials
29 audio clips (one is background music in m4a format around 5MB)

I don’t get why the format is shown as ogg in the Unity Editor when I used iTunes to create the m4a file from an mp3. However it is marked as iPhone Background music, so I guess there is nothing wrong here.

I do have one game object that has a DontDestroyOnLoad script and one static class, none of them holds reference to other gameobjects, but multiple gameobjects holds a reference for them.

Thank you for helping me out.

Two things to try:

  1. Before you quit your scene, explicitly stop any music, destroy all your gameobjects, free up your arrays and lists, and set everything to null.

  2. Instead of loading a separate scene, make the scene into a prefab and instantiate that instead.

Good luck!

M

Thanks for the quick reply.

Is it sufficient to null everything on onDisable on the game objects?

We have pretty similar memory consumption in Rimelands, and according to your texture etc counts, our game is at least 10 times larger than yours. It would seem Unity hogs all the memory it can at first, but then stops there.

One thing I noticed was that any references to the project folder (prefabs, etc) stay in the memory even after the scene which uses them is unloaded. At first my cutscene code had textures that were references to textures in the project folder, and after the scene was unloaded, the memory didn’t drop. Then I changed it so that all the textures are already inside GUITextures in the scene - and that caused the memory to actually go down after the scene change.

So I wouldn’t instantiate prefabs instead of scenes, as they will probably take more, not less memory.

When I run FindObjectsOfType(Texture) it returns all textures in the project, even though they are not referenced in the scene. Is this similar to what you noticed?

Can you explain in other words what you did to prevent that?

Thanks again.

Not for unity objects.
Use the Destroy / DestroyImmediate functions if you want to get rid of them