It was pointed out to me that this is probably a better suited forum for my questions than “Editor & General Support” so I’m recreating it here. Here is a link to the original thread.
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 10-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 two weeks trying to profile the game unsuccessfully. 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 15 seconds, unloads the second scene, waits 15 seconds and repeats.
private IEnumerator MyLoaderRoutine()
{
bool isInLevel2 = false;
float waitTime = 15f;
string sceneToLoadName = "SceneToLoad";
while (true)
{
yield return new WaitForSeconds(waitTime);
if (!isInLevel2)
{
yield return SceneManager.LoadSceneAsync(sceneToLoadName, LoadSceneMode.Additive);
}
else
{
yield return SceneManager.UnloadSceneAsync(sceneToLoadName);
yield return Resources.UnloadUnusedAssets();
System.GC.Collect();
}
isInLevel2 = !isInLevel2;
}
}
The Unity project in case someone is interested:
http://ellioman.com/temp/LoadSceneTestWithGC.unitypackage
I made two tests in 5.6.0b11. One for 5:30 seconds. Then another for 11 minutes.
Note: I have also tried this in 5.4.4p1 and the same issue occurs there.
Test 1
After 0:32 => 48,2MB
After 5:30 => 55,0MB
GIF: http://ellioman.com/temp/Unity560b11_Metal_WithGC_Duration_5min.gif
Test 2
After 0:32 => 48,2MB
After 5:35 => 55,5MB
After 11:00 => 55,9MB
GIF: http://ellioman.com/temp/Unity560b11_Metal_WithGC_Duration_11min.gif
Bonus: Test using the same script but loading a scene in my game.
After 0:35 => 148,0MB
After 5:45 => 164,0MB
After 8:55 => 173,0MB
GIF: http://ellioman.com/temp/Unity560b11_OriginalGame_Metal_WithGC_Duration_9min.gif
So my question is: Is this normal?
In the first two tests things seem to rise in the first 5:30 but almost nothing after that. But in my game it just keeps on growing.