Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to get find out if managed memory was allocated

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

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    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

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,483
    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.