Hey everybody,
Right now I’m the process of trying to locate a incredibly annoying memory leak in a game where the game, in IOS, sometimes adds 5-10 MB to the memory around 1-20 seconds after loading a level. The game doesn’t add the MB’s to the memory instantly but once it starts it takes around 5 seconds.
I have spent around 8 days trying to profile the game and I still have not found the leak. I did however notice something that seems odd and wanted to see if I found a bug and if it could be the culprit in the memory leak I’m trying to fix.
I created a project where I have two scenes:
- The startup scene. Has a camera, sprite and a game object with a script to load and unload the second scene
- This scene contains only numerous boxes and spheres. Nothing more.
My script starts a coroutine that loads the second scene additively, waits 4 seconds, unloads the second scene, waits 4 seconds and repeats.
private IEnumerator MyLoaderRoutine()
{
while(true)
{
yield return new WaitForSeconds(waitTime);
if (!isInLevel2)
{
yield return SceneManager.LoadSceneAsync(loadedSceneNam, LoadSceneMode.Additive);
}
else
{
SceneManager.UnloadScene(loadedSceneNam);
yield return null;
yield return Resources.UnloadUnusedAssets();
}
isInLevel2 = !isInLevel2;
}
}
I ran this project on my phone for 5:30, using Unity 5.4.4p1 and 5.6.0b11, and in both cases the memory does increase in the XCode debugger. I also tried this with UnloadSceneAsync and yielding to that but the problem was still there.
In the 5.4.4p1 build:
After approx 35sec, after unloading the second scene, the memory shows 59,3MB.
After 5 minutes and 30 seconds the memory is at 61.9MB after unloading the second scene.
(GIF: http://ellioman.com/temp/Unity544p1_Metal.gif)
In the 5.6.0b11 build:
After approx 35sec, the memory shows 47,6MB.
After 5 minutes and 30 seconds the memory is at 51.7MB.
(GIF: http://ellioman.com/temp/Unity560b11_Metal.gif)
Also made another test with 5.6.0 where the waiting time between loading/unloading was 15 seconds.
There it went from 38,9MB to 41,5MB
(GIF: http://ellioman.com/temp/Unity560b11_Metal_15sec.gif)
So my question is: Is this normal?
I exported the 5.6.0b11 project to a unity package if people wanted to test.
http://ellioman.com/temp/LoadSceneTest_Unity560b11.unitypackage