Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Reloading assemblies in the editor gets exponentially slower and slower

Discussion in 'Editor & General Support' started by tmcsweeney, Oct 17, 2014.

  1. tmcsweeney

    tmcsweeney

    Joined:
    May 24, 2011
    Posts:
    20
    I've got a strange problem going on. A lot of the time when modifying code and then alt+tabbing back to Unity, Unity will take longer and longer to reload the assemblies after importing them (during which it locks up), until eventually it becomes unusable. Going through the editor log file afterwards I'll see a pattern something like:

    - Completed reload, in 0.751 seconds
    - Completed reload, in 1.626 seconds
    - Completed reload, in 6.085 seconds
    - Completed reload, in 2.922 seconds
    - Completed reload, in 7.728 seconds
    - Completed reload, in 1.976 seconds
    - Completed reload, in 3.038 seconds
    - Completed reload, in 5.635 seconds
    - Completed reload, in 9.598 seconds
    - Completed reload, in 47.630 seconds
    - Completed reload, in 332.527 seconds

    Quitting and restarting Unity resets the problem temporarily, but after a dozen or so reloads it comes back.

    I can recreate it by repeatedly modifying a single integer constant in the code, thus forcing a recompile again and again. Recreation is not 100% reliable though, sometimes the problem will disappear by itself for a while.

    My first thought was that one of our editor extensions had gone rogue, and I've been trying to strip out bits of the project to narrow down the culprit, but the unreliable reproduction makes this tricky. I'm never sure if the problem has gone away because of the code I just removed, or if it has vanished for a bit of its own accord.

    Is anyone else seeing problems like this? We're getting it on both Mac and PC, in Unity version 4.5.4f1
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  3. tmcsweeney

    tmcsweeney

    Joined:
    May 24, 2011
    Posts:
    20
    Thanks Graham, while that doesn't sound like exactly the same issue, it has given me some hints of how to proceed with debugging it when I get back to work on Monday.
     
  4. tmcsweeney

    tmcsweeney

    Joined:
    May 24, 2011
    Posts:
    20
    I've got some more information on this issue, but it is still not solved.

    I went down a false path for a bit chasing up objects marked HideAndDontSave, which we are using for some debugging utils. However, even with that code completely disabled the problems still occurs.

    The problem only seems to happen (or progresses fast enough it becomes noticeable) in a large scene file with lots of game objects. Trying to replicate it in an empty scene results in the memory usage growing so slowly that I'm not sure if it is the same problem or the GC just being lazy.

    Using the "Profile Editor" option in the Unity profiler allows me to see the memory usage grow and grow exponentially each time I modify a script and reload assemblies. In typical run of 12 or so reloads (to the point where reload assemblies is taking almost 5 minutes) the memory usage grows like this:


    Code (none):
    1. Reload Time Total mem Unity mem mono mem  Total Object total object count
    2. 0.735       335.5     200.9     5.4       4498         16585
    3. 2.725       360.2     215.3     6.7       4508         16864
    4. 4.949       362.6     216.9     6.5       4522         16879
    5. 2.41        364.3     218.5     5.5       4548         16905
    6. 2.324       368.4     220.2     7         4610         16967
    7. 2.759       371.4     221.9     7.2       4780         17137
    8. 2.881       368.8     209.6     7.9       4788         16877
    9. 4.529       382.5     224.1     5.9       5282         17639
    10. 5.871       387.5     226.2     7.7       6748         19105
    11. 10.82       410       229       9.9       11130        23487
    12. 48.556      444.1     218.6     20        11138        23227
    13. 37.995      466       236.7     22.6      24268        36625
    14. 333.877     550       252.4     39.7      63648        76011


    It sure looks like a memory leak, but I have no idea how to track down what is leaking.

    If I try and grab a detailed memory snapshot with the full level loaded then the Unity editor just instantly crashes. However if I try grabbing snapshots after reloading assemblies in an empty scene then I can see that each reload results in a new Unity.PackageManager.EntryPoint appearing in the Not Saved/MonoBehaviour section of the detailed memory snapshot.
    the memory usage for Other/MonoDomain and Other/Managers/BaseObjectManager also keep growing slowly.
    I don't know if this is normal or not.
     
    Last edited: Oct 20, 2014
  5. tmcsweeney

    tmcsweeney

    Joined:
    May 24, 2011
    Posts:
    20
    For anyone who stumbles upon this in the distant future: I eventually located the problem by strategically disabling code that might be executing in the editor until the problem went away.

    Predictably it turned out be be me doing something naughty. I had an editor extension that was attempting to keep values synced between multiple ScriptableOjects. When the extension intialized (each time the code reloaded) it would construct temporary copies of the ScriptableObjects (but not turn them in to assets). I had blithely assumed that these objects would be garbage collected, but it appears this is not the case. Adding a call to DestroyImmediate() when the temporary objects were no longer needed fixed the memory leak.
     
unityunity