- Home /
Application.LoadLevel() doesn't unload current scene?
As in the title - in my game I load a new scene with Application.LoadLevel()
and when I profile the app with Xcode Instruments (or what's it called) it shows me under "Real Memory Usage" that it never unloads the previous scene. To be specific: 1st scene takes about 90 MB of memory and 2nd scene is only 1 object and 1 camera so should take much, much less.
(The rest of the post is explaining what I do in detail, if you already know what's wrong you might skip this part.)
Well, to be even more specific I have 3 scenes: Menu, Gameplay and Loading (the one with only 1 object - progress bar). When Menu loads completely "Real Memory Usage" shows about 90 MB. When I want to load Gameplay, the Loading scene is loaded with Application.LoadLevel()
. Loading scene does this:
using UnityEngine;
using System.Collections;
public class LoadingState : MonoBehaviour
{
public static string levelToLoad;
private UISlider slider;
void Start()
{
slider = GetComponent<UISlider>();
var text = GetComponentInChildren<SpriteText>();
text.Text = MenuState.jsonConfigRoot.GetString("loadingText");
print("LoadingState: Loading...");
StartCoroutine(LoadLevelWithProgress());
}
IEnumerator LoadLevelWithProgress()
{
var async = Application.LoadLevelAsync(levelToLoad);
while (!async.isDone)
{
slider.Value = async.progress;
yield return new WaitForEndOfFrame();
}
}
}
So it loads Gameplay with Application.LoadLevelAsync()
and display the loading progress.
And it works this way: When Loading starts "Real Memory Usage" shows me around 30 MB which instantly goes up as Gameplay loads. When done it takes about 75 MB. But when I want to come back to Menu (the same way: I load Loading with Application.LoadLevel()
which then loads Menu with Application.LoadLevelAsync()
), "Real Memory Usage" should drop again but it doesn't, it just keeps taking more memory (over 100 MB) and finally the app is killed by the OS as it apparently doesn't have that much memory (iPhone 3GS).
Any idea why it doesn't unload the Gameplay scene? Any way to force it to do so?
Are you absolutely sure the Gameplay scene is to blame? Could you have some static classes or managers sticking around that have allocated non-scene memory? $$anonymous$$aybe a pool manager, or loaded assets that somehow have references so they're not unloaded by UnloadUnusedAssets()? Or some objects that are marked DontDestroyOnLoad?
Does System.GC.Collect() have any effect? (It won't garbage collect Unity objects, but maybe the culprit is elsewhere.)
I'm not sure so I'll check that and let you know. As for System.GC.Collect()
- that was weird. It made my app keep using about 150 $$anonymous$$B whole time (no matter which scene was loaded. Btw I just noticed that today the app used more memory (in each scene) than when I wrote this post... not really sure why. :P
Your answer
Follow this Question
Related Questions
Unity3D app crashes out after error "Warning -> ApplicationDidReceiveMemoryWarning' 2 Answers
Unity ios build and device memory limit 0 Answers
iPhone Game Crashing During Application.Load 0 Answers
Large memory footprint increase when assigning a GUITexture from a WWW Object on iPad 1 Answer
How do I destroy things properly to free up memory? (iOS) 3 Answers