Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to solve the increase of Virtual Machine(mono)?

Discussion in 'Profiler Previews' started by cyj-teia, Dec 21, 2023.

  1. cyj-teia

    cyj-teia

    Joined:
    Dec 8, 2023
    Posts:
    2
    I am using memory profiler 0.7.1.
    Windows Standalone build

    Virtual Machine (mono) increases by 18.5MB every day.
    Managed Heap increases by 1.7MB every day.

    How to solve the increase of Virtual Machine(mono)?

    What kind of data is Virtual Machine (mono)?
    What kind of data is Managed Heap?
    What is the difference between the two?

    Thank you
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,492
    every day as in you run the app for multiple days?

    The VM memory is mostly used for Managed Type metadata (field and method tables). Taking a memory snapshot, the snapshot process goes over all objects currently on the heap to see if there are types which have not yet been initialized, and then we initialize them so that we can give you all the details you need once you open it in the tool. This initialization normally happens way ahead of taking the snapshot when the static constructor is invoked for the type, but some times you may have e.g. an empty array, list or other generic container that should eventually be populated by a class type without an object of that class ever having been created, nor their static fields accessed, and therefore the type remained un-initialized. In those cases, the snapshot process will increase the VM memory usage as it needs to initialize them. This growth might only be displayed in the next snapshot.

    I would expect this snapshot related growth to be a finite growth though as, at some point, all types that are getting any usage should be initialized.

    Beyond that, Reflection can initialze even unused type and inflate the VM Memory usage. Heavy use of generics (and worse still, combined with reflection) can lead to more VM memory usage.

    Also, Memory Profiler version 1.0/1.1 give you an explanation for what this memory is:
    Unity_Y2bx8O3uEP.png
    likewise to the above:
    Unity_3dZYl7IFPe.png

    The question that's left is if the Managed Heap memory usage growth is not related to Objects but to Empty Heap Space. If so, you might be seeing Manage Heap Fragmentation, that is "easier" to investigate in 0.7 using the Fragmentation page. It's pretty tough to try and avoid though without a very deep understanding of Unity/Mono's managed memory allocation patterns under the Boehm GC.

    If you are on a Unity version older than 2022.2, you can, btw, use version 1.1 of the Memory Profiler in an empty 2022.2/3 project and by importing the snapshots to that, or attatching that Editor to your Player and capturing them directly.
     
  3. cyj-teia

    cyj-teia

    Joined:
    Dec 8, 2023
    Posts:
    2
    Yes, builds must run for serveral months or years.
    The memory usage displayed in Windows Task Manager increases by 25 MB every day.

    We run GC.Collect() and Resources.UnloadUnusedAssets() around 3:00 every day.

    According to the profiler results,
    Since the Managed Heap increase is smaller than the VM increase,
    I think the cause is in the VM rather than the GC
    My guess is right?

    Thank you
     
    Last edited: Dec 26, 2023
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,492
    Where the growth occurs doesn't say anything too definitive about the cause. The cause is in your managed scripts somewhere.

    Calling GC.Collect should not be necessary as that's automatic.

    Calling Resources.UnloadUnusedAssets triggers something that is not automatic but instead triggered at key moments like Destructive Scene Unloads. It should generally not be necessary if you handle the lifetime of your Unity Objects correctly (Destroy() every Asset Type object you instantiate, Unload scenes occasionally or destroy Scene Objects you no longer need), though it's rather easy to make mistakes there. And while Unity Objects may have a small managed heap memory part to them, they are usually mostly comprised of native memory.