Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Understanding Memory usage

Discussion in 'Scripting' started by wachief, Dec 2, 2023.

  1. wachief

    wachief

    Joined:
    Jan 14, 2013
    Posts:
    4
    I am trying to figure out a breakdown of how much memory I am using in RAM and VRAM separately.

    I started by looking at the profiler documentation, but it does not specify what is the memory it returns is.
    For example, Profiler.GetTotalAllocatedMemoryLong provides: "The amount of memory allocated by Unity". Is that in RAM only or RAM and VRAM combined?

    Same question for Profiler.GetTotalReservedMemoryLong and for Profiler.GetRuntimeMemorySizeLong.

    If the values are RAM and VRAM combined, is there a way to get the information of how much RAM only is used?

    I am using version 2021.3 of the engine.

    Thanks.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,186
    Have you tried the memory profiler?
     
    lordofduct likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,488
    upload_2023-12-1_20-23-59.png

    upload_2023-12-1_20-23-38.png

    It has pretty graphs too!
     
  4. wachief

    wachief

    Joined:
    Jan 14, 2013
    Posts:
    4
    I have tried it. The information there is more useful but still somewhat vague.
    Also I can't really use it on a release build (not a development build). The project I am working on requires that the user will have insight to how much memory is being used and where it is allocated (RAM vs VRAM).
     
  5. wachief

    wachief

    Joined:
    Jan 14, 2013
    Posts:
    4
    I am aware of the profiler window. I need something that can work at run time on a non-development build.
    Also the description of memory there is vague. For example, is total committed memory RAM only or VRAM (GPU memory) as well?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,488
    Use the 'Profiler (Standalone Process)'? That allows you to connect to built versions of the game.

    If you want clarification of what the things mean... read the documentation.

    If you don't want them to have to be development builds... welp... uh, don't know what to tell ya. That's how you get the access to profiler stuff.

    Note the class you're referring to, 'Profiler', says:
    https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Profiling.Profiler.html

    So even the stuff in your OP won't necessarily work well in a non-development build.

    If you need remote access (say on device), development builds allow connecting the profile over your network, so that's not a problem really either.

    Lastly:
    It's all of the RAM if I recall correctly (read the documentation if you need). Note that the yellow bar in my image is ALL memory. Then below that is a colorful breakdown of all of that memory into categories. The solid colored portions of the bars is the used memory, the outlined portions is the allocated but not actually used at the moment (this reflects how memory allocation works when an OS is involved. Your application requests chunks of memory, and then uses a portion of that memory upto the total of memory requested).

    And of course there is the untracked memory... that's just memory that unity's profiler knows is allocated by the program, but doesn't really have any analysis of what it's being used for.

    Oh, and there is a package that does even more profiling:
    https://docs.unity3d.com/Packages/com.unity.memoryprofiler@1.0/manual/index.html

    I've used this to find really weird bugs. I used it recently for a bug that haunted one of my games for years... ugh... that was annoying.
     
    Last edited: Dec 2, 2023
  7. wachief

    wachief

    Joined:
    Jan 14, 2013
    Posts:
    4
    I have read the documentation. This is why I am asking here. It is not clear enough. The functions I asked about in the profiler class do work in release builds. But I need to make sure what the documentation means by "native-memory" and "total memory allocated".
    The documentation does not say if it is RAM or not and I don't want to assume.

    The profile editor window doesn't really help me with what I need since it can't work on a release build. I need to estimate size of assets that are created at runtime on a release build.

    To reiterate, I want to understand what the documentation is saying. just because it says memory doesn't mean it is RAM and the memory from the graphic card.
     
  8. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,405
    Technically, that one is more there for profiling the Editor. Either version of the Profiler can profile built versions but the Standalone Profiler is not for profiling Standalone build, but is a separate process thereby allowing profiling the Editor without recursively profiling the profiler as well.
     
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,405
    And Total committed includes VRAM. Also not all platforms have dedicated VRAM, so we don't call it that in the Profiler as it'd be inaccurate on Unified Memory architectures. Instead we call it "Graphics" but that means the same.

    Before 2022 we didn't split the GPU and CPU addressable memory up properly and per object, nor did we offer an API to get the total. That got remedied on 2022.2 with Profiling.Profiler.GetAllocatedMemoryForGraphicsDriver() but that's not available in Non-Development builds. So there's nothing there. If you absolutely need it, and have only one or few platforms that you want to ship on, you can dig into the platform documentation to see what APIs there are to query to get that total number from the GPU, implement a native plugin that uses those APIs and embedd that Plugin in your Unity Project.
     
  10. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,405
    You can also log your Feature Request to get that added to Unity here via the "Submit and Idea" entry