Search Unity

Managed Heap memory leak

Discussion in 'Editor & General Support' started by Doghelmer, Aug 10, 2018.

  1. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    Diving into my profiler, it's evident that ManagedHeap.UsedSize is continuing to expand as I run my game.

    I don't think I'm doing anything egregiously bad during gameplay. However, since my game is procedurally generated, a lot of things need to get instantiated at the beginning of a level.

    Here's the thing: My game doesn't require that the Unity scene be switched in order to change levels, since it's all procedurally generated. However, if I DO switch scenes between levels, this seems to prevent the memory from getting too high -- or at least alleviates the issue to a good degree. The downside to this is, in this particular case, that switching scenes causes my game to suffer slower load times.

    I'm not sure how much there is I can do with my code to prevent ManagedHeap.UsedSize from climbing. I've attempted pooling a lot of the things I was instantiating, but this only alleviates the issue rather than completely solving it.
    Having followed the guidelines on the this page, I'm not sure where to go from here.

    Is there no way to just reduce the managed heap? Meaning, achieve something similar to a scene change, without actually changing the scene?
     
    Last edited: Aug 10, 2018
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    813
    It's difficult to diagnose memory issues without knowing all the gritty details of how you've implemented your systems. However, if reloading the scene fixes the problem, then you're probably not doing something that is allowing all of your old objects to be completely unloaded from memory. Do you use many non-MonoBehaviour classes?
     
  3. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    I'm using non-monobehavior classes here and there, though some of them may be attached to a large number of objects. Is this something that should be avoided? Is there an ideal way to unload these from memory?

    Also - Upon further inspection, reloading the scene doesn't necessarily "fix" the problem as well as I'd like. There have been points where reloading the scene actually resulted in increased memory usage, though this wasn't common.
     
    Last edited: Aug 10, 2018
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    813
    Are you profiling the editor, or a built game?
     
  5. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    I've profiled both, the same rise in memory usage occurs in either case.
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    813
    Off the top of my head, I'm afraid I can't give you more specific advice other than to make sure you have a good handle on the lifecycle of all of the objects you're creating. It's possible that somewhere in your code you are accidentally keeping something in memory and making GC unable to collect it. It's also worth seeing if
    Resources.UnloadUnusedAssets
    does you any good.
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    4,420
    Madgvox likes this.
  8. sagund

    sagund

    Joined:
    Sep 5, 2019
    Posts:
    1
    @Peter77 I have a similar issue as Doghelmer is facing. But in my case memory rise happens only in the application and not in the editor.
     
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    4,420
unityunity