Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    3
    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:
    3
    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:
    128
    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.