- Home /
Why is the memory used of a level going up when entering the level from another level?
So I'm in publishing mode and getting ready to do a final launch of my game and I'm trying to determine what minimum device specification is needed for the game to work perfectly since it's a paid game and I don't want people to download it and not have it work. Well when I start the editor when I'm in a level it will say total memory used 312mb or something around that, and then I exit the editor and go to another level, start the editor and the memory used it 200-300 mb, which is great by me (I'm going to work on making that substantially less in the next update), anyway, when I start testing the full game and I'm in one level the memory used matches what I checked the first time then when I click to go to the next level, the memory spike up, its suddenly 600mb-700mb, and going back and forth it increases a little each time, do I have something wrong? I checked online and one post that's pretty old says that mlnodevelop does not handle garbage collection to well and does clear up used variables once they are no longer in use, is that still true with unity 5? Any tips on how I can make this better?
Answer by SarperS · Aug 13, 2015 at 10:57 PM
Unity does not clear the previous level until the new level is loaded, and after that it slowly clears it up each garbage collector cycle. A well known trick is to put a very simple loading scene in-between scenes. So you go to the loading screen, it clears up the previous scene while loading the next scene, next scene takes less space because the only garbage is from the much simpler loading scene.
That makes a lot of sense, so I'm currently using loadlevelasync to give a loading screen, do you think I could implement your idea by making a loading scene that has a script that gets a playerpref string and loads the level of that string, and I set the strong when a certain button is pressed?
Sure, below is how I did it and I think it's a better alternative to PlayerPrefs. Let me know if you don't understand how it works.
using UnityEngine;
using System.Collections;
internal sealed class Loading$$anonymous$$anager : $$anonymous$$onoBehaviour {
private static Loading$$anonymous$$anager instance;
internal static Loading$$anonymous$$anager Instance {
get {
if(instance != null) return instance;
instance = FindObjectOfType(typeof(Loading$$anonymous$$anager)) as Loading$$anonymous$$anager;
if(instance != null) return instance;
var container = new GameObject("Loading$$anonymous$$anager");
instance = container.AddComponent<Loading$$anonymous$$anager>();
return instance;
}
}
[SerializeField] private UIProgressBar loadingBar;
[SerializeField] private ThreadPriority loadingPriority;
private static string sceneToLoad;
internal static string SceneToLoad {
get { return sceneToLoad; }
set { sceneToLoad = value; }
}
private void OnEnable() {
Application.backgroundLoadingPriority = loadingPriority;
StartCoroutine(LoadAssignedScene());
}
private IEnumerator LoadAssignedScene() {
// Wait a single frame so the frame gets rendered
yield return null;
// Start the loading operation
var loadingOperation = Application.LoadLevelAsync(SceneToLoad);
while(!loadingOperation.isDone) {
loadingBar.value = loadingOperation.progress;
yield return null;
}
yield return loadingOperation;
}
}
Thanks a lot! I'll give both a try and let you know which one I go with
I've tried it finally but it still doesn't work right, watching the memory profile it seems that it works right when it goes from the game level to the main level, but the main level to the game level the memory still spikes dramatically, the game level started alone used about 450mb, but when loaded from the main menu it spikes up to .75gb (the main menu and the loading scene are around 230mb so it makes sense) however I'm curious to know that even while playing the game scene the memory still doesn't go down, it stays at .75gb. Before I implemented what you said the game level was 1.1 gb so it def helped, however I'm still confused as why it didn't go down to what it is when I start the editor from that level. From what I understood unity should have cleaned it up when the level starts, but well into the level it stays the same...
Did you try profiling the game from the mobile device? Unity Editor might be caching the scenes (a total wild guess here).
Your answer
![](https://koobas.hobune.stream/wayback/20220611235733im_/https://answers.unity.com/themes/thub/images/avi.jpg)