Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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

Discussion in '5.6 Beta' started by ElliomanGames, Mar 10, 2017.

  1. ElliomanGames

    ElliomanGames

    Joined:
    Jul 26, 2016
    Posts:
    14
    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:
    1. The startup scene. Has a camera, sprite and a game object with a script to load and unload the second scene
    2. 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.
    Code (csharp):
    1. private IEnumerator MyLoaderRoutine()
    2. {
    3.   bool isInLevel2 = false;
    4.   float waitTime = 15f;
    5.   string sceneToLoadName = "SceneToLoad";
    6.  
    7.   while (true)
    8.   {
    9.     yield return new WaitForSeconds(waitTime);
    10.     if (!isInLevel2)
    11.     {
    12.       yield return SceneManager.LoadSceneAsync(sceneToLoadName, LoadSceneMode.Additive);
    13.     }
    14.     else
    15.     {
    16.       yield return SceneManager.UnloadSceneAsync(sceneToLoadName);
    17.       yield return Resources.UnloadUnusedAssets();
    18.        System.GC.Collect();
    19.     }
    20.     isInLevel2 = !isInLevel2;
    21.   }
    22. }
    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.
     
  2. ElliomanGames

    ElliomanGames

    Joined:
    Jul 26, 2016
    Posts:
    14
    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.
     
  3. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    655
    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 :)
     
  4. ellioman

    ellioman

    Joined:
    Mar 14, 2013
    Posts:
    19
    Hey Rupert,

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

    If yes, then yes I have :)
    Unfortunately I could not see anything there that was of any help. So far I have tried:
    1. Unity Profiler in Editor
    2. Unity Profiler On Device
    3. Unity's Memory Profiler
    4. XCode Debug
    5. 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:
    Screen Shot 2017-03-13 at 00.15.01.png Screen Shot 2017-03-13 at 00.15.07.png

    Along with these two threads on the Unity forums and answers:
    Now I'm starting to wonder if this is a bug?
     
    Last edited: Mar 12, 2017
  5. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    655
    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 ;-) 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 :)

    Good luck!
     
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,650
    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.
     
  7. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,143
    Hi ellioman,
    Could you please file a bug report with a minimal reproduction case for this issue and reply in here with the case #?