Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Is it normal for Mono to slowly increase in size in the memory profiler?

Discussion in 'Editor & General Support' started by BasicallyGames, Jan 31, 2019.

  1. BasicallyGames

    BasicallyGames

    Joined:
    Aug 31, 2018
    Posts:
    5
    In my game, obviously I want to keep memory usage as low as possible, and most importantly avoid memory leaks. However, looking at the memory profiler I've noticed that the value for mono slowly increases over time. Currently, it starts at about 14.3 mb, and then increases to about 21.0 mb after about 5 minutes or so, before going back to it's initial value (When I assume the garbage collector kicks in). Now, this definitely seems like it's probably not a huge issue, but if there's a way to avoid it then I'd like to take care of it now and figure out what's causing it, so I can avoid the issue going forward and prevent making it even worse as I add more objects and scripts. Oh, and I'm testing this in a build so I know the profiler isn't tracking any editor scripts.

    My scene has very few updates running in it, and the objects that do aren't creating any new objects or instances or anything. So what I'm wondering is, is this just normal behavior with Unity or C#, or is it likely that something somewhere is leaving behind unnecessary data? For example, my camera script, one of the few objects with an Update function (Technically LateUpdate), has a local int created each time it is called. Could this be part of the issue (Assuming this is an issue)? Does this int not get automatically removed from memory when the function finishes, and if not should I be doing so manually? Similarly, my player movement script contains "playerRotation.eulerAngles += new Vector3(xStuff, ySuff, zStuff)" in its update. Does playerRotation's previous vector3 never leave memory until the garbage collector is called? Should "new" be avoided in updates?

    These are the only things I can find that I think could be causing this issue, again, assuming it is an issue. I also use Rewired for handling input, so it's possible that is also contributing. I've tried doing some research on the memory profiler and memory leaks, but I can't seem to find any guides or tutorials explaining how to best avoid memory leaks in scripting (Besides some of the very obvious things) and what is considered normal in regards to how quickly memory usage increases. Thanks in advance for any help!
     
  2. BasicallyGames

    BasicallyGames

    Joined:
    Aug 31, 2018
    Posts:
    5
    Bump. I'd really like to know more about this. Anyone know of any helpful resources related to this that I could check out? Like I mentioned in the OP, I can't really seem to find much about this stuff when doing my own research online.
     
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    239
    the int and the struct that are created should land on the stack. Basic data types and structs usually don't allocate on the (garbage collected) heap, arrays, strings and classes do. That is unless boxing occurs.
    maybe this article here helps in figuring out what might allocate: https://unity3d.com/learn/tutorials...ion/optimizing-garbage-collection-unity-games

    The CPU profiler shows managed heap allocations as GC.Alloc samples. They are magenta in the CPU Timeline view and can be searched for in the Hierarchy view. To narrow down the search, you can use deep profiling or turn on allocation callstacks while profiling in the editor. The option is in a dropdown of the profilers toolbar. When selecting GC.Alloc samples in the timeline view, the tooltip will give you the callstack. Alternatively you can open the Details view in the Hierarchy view and set it to "Show Related Objects". When selecting a GC.Alloc sample and it's corresponding N/A object, this view will also show the callstack.
     
  4. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    608
    Im seeing the same thing too in 2018.1.

    To repro, select New Scene, (Main Camera + Direction light only in the scene).

    Open the profiler, click run. You can see the TOTAL MONO memory across the top of the profiler starting at around 150 MB and it keeps going up and up.

    I saw the same thing when i built for WebGL, the memory usage of the browser keeps going up too even if nothing going in on the game. Open the task manager in the browser itself from the browser menu (chrome) and it shows the memory usage of the unity app keeps going up and up.
     
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    239
    Hi @nsmith1024
    does this sound like your issue? https://issuetracker.unity3d.com/issues/windows-memory-leak-on-net-4-dot-x-and-x86-builds
    Also, please ignore the result of profiling the editor unless hitting "Clear" does not reset the amount of memory that "Unity" claims to be holding on to. The profiler in current versions does not necessarily allocate the profiler frame data stream under the "Profiler" category correctly in current version (2018.3-2019.3.0a7) so that might look like a leak that is none.

    Also, generally speaking: The mono heap does never get compacted so you could have memory fragmentation going on that continuously grows the heap and leaves it so fragmented that new stuff gets allocated in new address ranges.

    You'll want to be looking at memory snapshots using the new Memory Profiler Package https://forum.unity.com/threads/new...ew-package-available-for-unity-2018-3.597271/ and specifically the Memory Map view. You could take two snapshots with a bit of time in between and diff them. The "Memory Map Diff" View will then help you find out where your memory grew and how. If you have any issues with this, further questions or want to check if the issue is within your project or unity, leave a post in this sub-forum: https://forum.unity.com/forums/profiler-previews.199/

    Also, please always state what packages and plugins you have in your project and if it is an empty project folder additionally to an empty scene. There not being anything in the scene doesn't mean that it is a bug with Unity or Mono as code can hook into the playerloop without having direct references in the scene.