Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trying to find potential Memory Leak in Server

Discussion in 'Editor & General Support' started by TeeArr, May 22, 2023.

  1. TeeArr

    TeeArr

    Joined:
    Jul 19, 2019
    Posts:
    10
    So I've deployed a Unity Server and it's slowly taking up more and more memory even though there's no activity on the server.

    I've used the new Memory Profiler to get to the bottom of things but comparing 2 snapshots between 5 days didn't really tell me much. (See attached image)

    Unity Objects and Objects in general didn't grow much, but the Virtual Machine, Empty Fragmented Heap Space and "Unknown" memory grew in size. Is this an issue with the engine? Or do you have other tips how to find the leak?

    (The server is supposed to run for weeks or maybe even months at a time, so even a relatively small leak could become problematic)

    The server is a dedicated Linux Build (Mono), but I've seen similar behavior on a Windows build as well.
     

    Attached Files:

  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,121
    The Virtual Machine memory growing could be a side effect of taking memory captures, which initiates type metadata. That is addressed in newer Unity versions but should also level off after a few captures.

    The fragmented heap space can become an issue if there are some small, longer held managed allocations that remain in a heap sections after that is otherwise empty. Empty spaces will be reused if there is enough contiguous space for a new allocation in it, but if that space is broken into smaller bits by lots of small, long lived allocations, then new space is needed instead and empty spaces can be wasted. Given that the currently used Boehm GC is non-compacting this can become a bigger issue over time.

    The best thing you can do to avoid this is to avoid building up small long lived allocations. So if you go to the All Off Memory view and find out what those 2MB of Scripting objects are you can check if these are necessary to be allocated and kept, if they can be avoided, or if they can be (pre-)allocated in chunks that would lead to less fragmentation.

    As for the Unknown amount: what Unity version are you on? Generally the newer versions should have less of the memory usage unknown.
     
  3. TeeArr

    TeeArr

    Joined:
    Jul 19, 2019
    Posts:
    10
    Thanks for the insightful reply. I'm currently on version 2022.2.19f1
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,121
    Bit of a late reply but iirc there have been some additional fixes backported to 2022.3 to close some untracked reporting gaps.
    In 2023.1+ you can also get some more insights into the untracked amount as we report information on the memory pages that fall into the untracked amount.

    Some things that remain untracked as of 2022.3 are:
    • Memory allocated within native plugins that do not use the Native Plugin API for our Memory Manager (to fix that, please contact the developers of these plugins and ask them to integrate that native plugin API into their plugin)
    • Allocations done via Marshal.AllocHGlobal
    • Bugs/reporting gaps in our native code
    • ... Possibly more Unknown/Unknowns?