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. The 2023.1 beta is now available for testing. To find out what's new, have a look at our 2023.1 feature highlights.
    Dismiss Notice

How to get find out if managed memory was allocated

Discussion in 'Profiler Previews' started by liortal, Oct 25, 2019.

  1. liortal


    Oct 17, 2012
    Sorry for posting here, my question is not related to beta, but this forum is dedicated to profiling.
    I'd like to know what API should be used to understand how much (managed) memory was allocated?

    From what i saw there are a few different APIs, not sure which is the most accurate one:
    *Please note that i would like this to be used from the context of an editor extension so this should work out of play mode.
    tigerleapgorge likes this.
  2. MartinTilo


    Unity Technologies

    Aug 16, 2017
    We do have the profiling tag on the forums which I and others in the profiler team follow ;)

    Anyways: as the naming in the APIs you mentioned indicates somewhat: those APIs give you what the Memory Profiler Module shows as "Total" value, i.e. Native + Managed.

    If you want to get the managed memory only, you should be looking at
    • Profiler.GetMonoUsedSizeLong for how much is already occupied and how close you are to expanding the heap
    • Profiler.GetMonoHeapSizeLong for what the current cap for the heap is, where exceeding that cap will cause a GC.Collect and potentially heap growth, and also for what the OS sees in terms of managed memory.
    Not that those are not going to report Native allocation from Mono itself, so no reporting on C# Type Metadata that would be allocated for Reflection stuff, only the managed heap.