Unity 5.6.0b11 - Memory leak when loading and unloading scenes?

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.

Another thing I have noticed is that I only see this apparent “leak” in Xcode but not when profiling in Instruments.

I tried disabling the “Enable backtrace recording” but the problem is still shown in XCode.

I’m no leak expert nor iOS guru so just 1 suggquestion:

Have you considered applying the IL2CPP Memory Profiler? After a rocky start, I’ve been loving the detail it can provide!

HTH, Rupert.
P.s. curious to hear what you discover since I’m using asynchronous additive loading in a WIP android game of mine. Not profiled yet though :slight_smile:

Hey Rupert,

I take it you mean this one?
https://bitbucket.org/Unity-Technologies/memoryprofiler

If yes, then yes I have :slight_smile:
Unfortunately I could not see anything there that was of any help. So far I have tried:

  • Unity Profiler in Editor

  • Unity Profiler On Device

  • Unity’s Memory Profiler

  • XCode Debug

  • XCode Instruments

Haven’t been able to figure out where the problem may be.

I did however notice tonight this in the “Leaks” tool inside Instruments:

Along with these two threads on the Unity forums and answers:

Now I’m starting to wonder if this is a bug?

Well good to hear you may be on the road to fix-ville. I guess you’re now checking whether it happens off iOS?

If that doesn’t pan out and if you use that Memory Profiler you correctly identified from my vague reference :wink: and if it might be 2D array problem (yeah unlikely), you might want the tiny patch mentioned in this post (under third issue)!
But probably not :slight_smile:

Good luck!

Make sure to turn off GPU Frame Capture and Metal API Validation in Xcode while checking for memory leaks. I observed having these debug options enabled cause memory leaks in Metal, which I believe is fixed in iOS 10.1.2 and later.

Unity has an useful article how to find memory leaks on iOS, see here.

Hi ellioman,
Could you please file a bug report with a minimal reproduction case for this issue and reply in here with the case #?