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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Memory profiling question: "Used Total" vs "Total System Memory Usage"

Discussion in 'Editor & General Support' started by SullyTheStrange, Mar 26, 2017.

  1. SullyTheStrange

    SullyTheStrange

    Joined:
    May 17, 2013
    Posts:
    147
    I'm pretty new to memory profiling so I've never had to think too hard about these labels in the profiler, and I can't seem to find a exact explanation for them online. What EXACTLY is the difference between these two?

    I see that UT is always much smaller than TSMU, but what exactly does each refer to? I thought maybe UT is how much my actual game is using, separate from Unity and all of its components listed there, but adding them all up doesn't match with TSMU either.

    Basically what I'm really trying to figure out is, which one should I be worrying about? Is TSMU one of those cases where Unity will free up some of that if necessary? So if the end-goal device has 1GB of available RAM but TSMU is >1GB, it won't necessarily crash, but if Used Total is >1GB, it definitely will?
     
    alexeyzakharov likes this.
  2. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    Hi @SullyTheStrange,

    Thanks for the question! I'll explain memory stats here.

    Unity Editor and Players internally use pools for all memory allocations. E.g. when allocating 16 bytes we ask system to allocate 4MB (in the Player) or 16MB (in the Editor) block and then all subsequent allocations use that memory. If block is full, we allocate another one and if it's empty we return it to the system. This technique is used to improve performance and reduce fragmentation.
    And also this makes stats a bit confusing.

    Used Total - memory used right now if we don't take pools and allocation headers data into account. Essentially it is sum of all internally managed allocations which are not freed yet. Profiler.GetTotalAllocatedMemoryLong is the Scripting API getter.

    Reserved Total - is how much we took from the system. This includes all allocated memory including pools and extra allocation headers. In our case it is commit memory - we allocate on heap (reserve+commit). Includes Used Total. Scripting API equivalent - Profiler.GetTotalReservedMemoryLong.

    Total System Memory Usage - total size of used (commit) memory by Unity process. This includes everything what is used by the process - Reserved Total + executable's images (code and data). It is PrivateUsage of PROCESS_MEMORY_COUNTERS_EX on Windows.

    Taking this into account in your case I would say that if Reserved Total >1GB player might definitely run out of memory on end-goal device. If TSMU>1GB system might page some some memory to disk, but that's not guaranteed and it might make performance horrible. So it is always safer to have TSMU<1GB.
    I would say TSMU is what should be mentioned as Required Memory in System Requirements for your game.
    (That's assuming you have TSMU data from the player.)
     
  3. SullyTheStrange

    SullyTheStrange

    Joined:
    May 17, 2013
    Posts:
    147
    Thank you, that clears things up!

    Interestingly on my device in question (a console), when profiling on the device itself, TSMU actually displays the full amount of memory I can possibly use, about 0.7gb of the system's 1gb. Makes sense since unlike PC the console only has to worry about my game occupying that space and nothing else. That sort of clued me in that TSMU is the important number on PC too. But it's nice to know what each label really means!
     
  4. daisySa

    daisySa

    Joined:
    Dec 29, 2011
    Posts:
    341
    We're seeing a weird value for Total System Memory Usage in 5.6.5p1 on Windows 10:

    Used Total: 7.61 GB Unity: 5.15 GB
    Reserved Total: 12.04 GB Unity: 8.41 GB
    Total System Memory Usage: 1.52 GB

    How can Total System Memory Usage be less than Reserved Total?
     
  5. chaosmaker

    chaosmaker

    Joined:
    Jul 21, 2013
    Posts:
    29
    upload_2018-12-26_11-48-52.png

    How can i get the "Used Total" via scripting in 2017.4?

    Scripting: Added new GetAllocatedMemoryForGraphicsDriver API (896812) this added in 2018.1, why not in 2017.4 ?
     
  6. jinwansu

    jinwansu

    Joined:
    May 25, 2018
    Posts:
    3
    hi @alexeyzakharov , i was wondering whether the API Profiler.GetTotalAllocatedMemoryLong is with the same meaning as "Used Total" in profiler or this API refers to total memory used only by unity engine (which equals the value after "Unity: ", which is in the same row as "Used Total" on the profiler panel).

    I took a snapshot at runtime by the API UnityEngine.Profiling.Memory.Experimental.TakeSnapshot(), and get the amount of memory of RenderTexture which rank 1st among native objects. RenderTexture takes 93.2 MB memory. And at the same time, I called the API Profiler.GetTotalAllocatedMemoryLong, it returns the value 66.22 MB, which is smaller than 93.2 MB.

    Sincerely anticipating your reply.
     
    Last edited: Sep 1, 2019
    alexeyzakharov likes this.
  7. alexrvn

    alexrvn

    Unity Technologies

    Joined:
    May 16, 2017
    Posts:
    53
    Hi @jiwansu internally we use the same API for reporting the memory of a render texture to the snapshot. Are you perhaps looking at the whole RenderTexture category in the TreeMap instead of the individual items inside?
     
  8. jinwansu

    jinwansu

    Joined:
    May 25, 2018
    Posts:
    3
    Thank you very much for your reply, @alexrvn !

    Sorry that my expression was indistinct.

    Actually, my point is that Profiler.GetTotalAllocatedMemoryLong() means "used unity" not "used total",which is different from @alexeyzakharov 's point of view.
    I was anticipating that you can confirm which one is correct.
    And the render texture thing was just showed as my proof, not the key point.
     
  9. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    I misunderstood the question as related to the RenderTexture :)

    Profiler.GetTotalAllocatedMemoryLong() returns the memory used by Unity and tracked by Unity. The closest equivalent in memory stats area is "Unity:". Which is -
    Unity: 38.7MB = Profiler.GetTotalAllocatedMemoryLong() + "untracked memory" - Profiler - FMOD - Video
    , where "untracked memory" are allocations done by some thirdparty libraries and OS.

    This is messy, yes and right now we are fixing it with a new memory profiler.
     
  10. fremsk

    fremsk

    Joined:
    Aug 24, 2013
    Posts:
    2
    Hi @alexeyzakharov,
    it's possible to get Profiler, Audio and Video memory usages from code?
     
    alexeyzakharov likes this.
  11. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    Not at the moment - but we are working on a new api to query all available profiler stats in the Editor and Player.
    (*We do have atm only undocumented
    UnityEditor.Profiling.ProfilerDriver.GetOverviewText(ProfilerArea profilerArea, int frame)
    which you can use in the Editor to get the whole memory stats text with ProfilerArea.Memory and parse - but not simple and clear getters for stats you mentioned).
     
  12. laessnb

    laessnb

    Joined:
    Jun 10, 2014
    Posts:
    101
    @alexeyzakharov We are seeing something similar in a standalone development build: used 1.58GB, reserved 1.75GB, TSMU 1.42GB (lower than both?). It's making it difficult to understand since I don't know how these are supposed to relate. Built using 2018.4.2f1.
     
    Sylvanas likes this.
  13. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    280

    Hi, with an empty project i have TSMU over 1.3GB, it is normal??? :S
     
  14. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    In the Editor or Player? It is normal for the Editor, not so for a Player
     
  15. Exbleative

    Exbleative

    Joined:
    Jan 26, 2014
    Posts:
    216
    Should doing GC and Resources.UnloadUnusedAssets cause Reserved Total to fall? It isn't in my tests. Or will that stay up at the previous highest level until Windows or something else tries to grab memory from somewhere?

    And if we had some sort of memory leak, would that figure be included in the Used Total or Reserved Total or neither?
     
  16. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    Resources.UnloadUnusedAssets might cause Reserved Total to decrease. As mentioned above Unity uses under the hood allocators which use larger memory pools for small allocations (e.g. 4MB blocks for allocations up to 1MB). The 4MB block is returned to the system only if it doesn't contain any used small allocations within. Returning such block would reduce Reserved Total. Unfortunately memory fragmentation might happen if resources with different lifetime are loaded together. And then large block can be stuck for some time until major cleanup happens (e.g. loading new scene exclusively).

    If you have a leak then both values should grow synchronously you can use any of those.
     
  17. MOBILIN

    MOBILIN

    Joined:
    May 6, 2017
    Posts:
    15
    Hi @alexeyzakharov,

    Would you explain me about the attached profiler status? I cannot understand why TSMU is so big. I cannot figure it out how to solve it. While move to the next scene, app gets down because of memory lack I guess. [Galaxy S9, Unity 2019.4.0f1]
    profiler_memory_20200615.png
     
    Last edited: Jun 14, 2020
  18. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    Could you please capture detailed snapshot to see if there is more information available.
    Also Memory Profiler can help with checking out native memory usage.

    From the screen you've provided I can see that both native and managed allocations tracked by profiler can't contribute to the system memory usage. Do you happen to use native plugins which allocate memory or similarly java level code which allocates?
     
  19. MOBILIN

    MOBILIN

    Joined:
    May 6, 2017
    Posts:
    15

    I had a problem to get the below screenshot because app had been crashed. It was caused by IL2CPP. After changing to mono, app keeps alive anyway.

    Please give me any comment about the screenshot status. Which one is real memory information that my app allocates? The total of Detailed is only 202.9MB, but TSMU says 3.28GB.

    upload_2020-6-18_0-26-39.png
     
  20. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,168
    Both are real. The Total value is the Total value as tracked by Unity's memory manager. The TSMU is the Total Memory that the OS reports as belonging to the Unity application; that includes Memory allocated in native Plugins that you might be using such as WWise, FMod, basically anything in your Plugins folder. The native Memory allocated by these plugins is not reported to Unity's memory manager and therefore can't be part of the Total number.
     
    idyakonov and cw222kq like this.
  21. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    @MartinTilo Hi. I have similar issue that my app shows TSMU 1.6GB even app is initially loaded with boot scene (almost empty except some UIs). Used Total is only 130MB. Is there any recommended solution for debugging what makes these large usage?

    Edit: (Unity 2019.4.4f1 + Android)
    Unity UsedTotal : 120 MB
    Unity TSMU : 1.68 GB
    Android Studio Profiler : 310 MB

    Unity's TSMU is even very different from Android Profiler.
     
    Last edited: Aug 3, 2020
  22. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    Even URP sample scene only app makes huge TSMU. I sent sample project as Case 1267773.
     
    MartinTilo likes this.
  23. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,168
    Thanks for the bug report, we'll look into it. We want the TSMU to be as closely as possible alligned with what the Platform profilers or Task managers report.

    Sorry the bug got initially closed by QA, it's a rather recent change that we doubled down on getting this alligned and supported a cross all our platforms.

    If you see my such misalignment on any platform or reports of 0 TSMU, please file another bug report so we can investigate that :)
     
    Kichang-Kim likes this.
  24. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    I made a thread here sometime ago because I didn't want to bump this thread prior to previous responses.
    I was seeing TSMU taking upwards of 5-6GB and my Used Total would hit 4GB and crash.

    When using the Memory Profiler, I couldn't find anything outrageous besides Textures (1GB) but it couldn't possibly account for all that memory being used. These were debug builds and I would like to continue using them.
    For short term, I destroyed the resolution of all my textures to keep my development going, but it's only a matter of time until I hit that issue again.

    The numbers on top seem extravagant compared to the numbers listed in the profiler tree (which is not even 2GB)

    Code (CSharp):
    1. Used Total: 3.43 GB   Unity: 1.50 GB   Mono: 321.3 MB   GfxDriver: 1.03 GB   Audio: 4.0 MB   Video: 0 B   Profiler: 0.58 GB
    2. Reserved Total: 3.83 GB   Unity: 1.78 GB   Mono: 424.4 MB   GfxDriver: 1.03 GB   Audio: 4.0 MB   Video: 0 B   Profiler: 0.60 GB
    3. Total System Memory Usage: 5.77 GB
    4. Memory Profiler Tree view:
    5. Textures: 1GB
    6. Animation: 260MB
    7. Mesh: 190MB
    8. RenderTexture: 100MB
    9. Font: 60MB
    10. Shader: 60MB
    11. MonoScript: 66MB
    12. Particle System: 38MB
     
    Last edited: Aug 18, 2020
  25. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,168
    Sorry, must have missed that thread while trying to get back on top after my vacation. Your TSMU looks roughly in line with the rest of the numbers reported so unless it contradicts what Android specific tooling tells you, it's a bit of a different issue and I don't see the connection to this thread here.

    I replied in that thread.
     
    CPlusSharp22 likes this.
  26. daniFMdev

    daniFMdev

    Joined:
    Jun 21, 2017
    Posts:
    7
    I've bee trying the new memory profiler in the preview package and it doesn't solve this problem, as it doesn't tell you the total memory used by the app.
    As of today, is there any way to get the "Used Total" value you get in the old profiler in runtime?
    How reliable is that value anyway, if my goal is to know the total memory my app is taking?
     
    chrismarch likes this.
  27. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,168
    We'll be adding these kind of overview numbers to the Memory Profiler package UI and yes it's a gross oversight that they aren't in there already. The initial focus of the tool was more on providing a more in detail and depth view of memory and the high-level overview was neglected on that path.

    Sorry, I'm not sure I can follow that question correctly. Do you want to get that value in the Memory Profiler UI or in the runtime? If the former, see above, if the later:
    • Profiler.GetTotalAllocatedMemoryLong
    • As of 2020.2 ProfilerRecorder API can query the Memory Profiler stats "Total Used Memory" and Total Reserved Memory" and the other stats, some like these high-level counter values even in Release players. We'll be updating the Memory Profiler Module manual page for 2020.2 with more details on these stats either next week or the one after that.
    That kinda depends on how that particular platform counts your memory usage. Usually, Reserved Total is going to be closer to what the platform considers as used memory, because it includes the pre-allocated buffers. And Total System Used Memory is what the OS itself says the app is taking (except for Android where it is currently the total memory used on the entire phone by any app and in the fix will be the Residual shared memory, because starting from Oreo, getting the total app used size from the OS comes at a steep price of ~500ms per request...)
     
  28. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    465
    Hello @MartinTilo ,
    Can you please clarify:
    1. Which version of Memory Profiler and/or Unity will/does include this Android "fix" to report Residual shared memory as TSMU (in the main unity profiler, in the memory module?)?
    2. Is Residual shared memory the most accurate measure that Unity has to predict low memory warnings on Android? Or is there something better?
    3. What is the best numeric metric available from scripts running in a Unity application on iOS and Android to predict low memory warnings?
    4. What is the best metric available from scripts running in a Unity application on iOS and Android to predict low memory warnings in non development builds? (Looking for something accurate, like https://developer.apple.com/documentation/os/3191911-os_proc_available_memory?language=objc on iOS)
    5. If there is a way to query TSMU on Android and iOS without a native plugin, and if not, any plans to add this to the API?
     
    Last edited: Nov 6, 2020
  29. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    465
    Is Scripting API equivalent supposed to give equivalent values to what the editor profiler memory module (simple view) displays under Reserved Total? Because,
    Profiler.GetTotalReservedMemoryLong() gives me a value that is over 100MB less than what the connected profiler shows there, with Unity 2019.4.13f1 running a player on iOS 13.4.1 on an iPhone 11.
    Should I just give up on Unity 2019 scripting API for useful memory statistics and write native plugins to call
    os_proc_available_memory and task_info, and Android equivalents?
     
  30. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    Hi @chrismarch !

    2020.2.0b8 has a fix for TSMU
    On Android we use rss stat obtained via proc/self/statm as this seems to be the fastest and most complete memory information. On iOS it is resident_size of TASK_BASIC_INFO
    The fix patches TSMU value in Profiler Window and ProfilerRecorder api (ProfilerCategory.Memory, "System Used Memory" counter).

    android:os:Debug::GetMemoryInfo
    is more accurate, but latency in my tests were around 100ms to get the data, so I've fallen back to much faster rss option.
     
    MartinTilo and chrismarch like this.
  31. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    We have plans to add a total device memory, so that together with "System Used Memory" (TSMU) that can give you better gauge of how much of system memory Unity app uses. (The C# api for that will be ProfilerRecorder api.)
     
    nihaan likes this.
  32. nihaan

    nihaan

    Joined:
    Nov 11, 2018
    Posts:
    7
    how to decrease Total System Memory Usage in nunity version 2019.4.4??
     
  33. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,168
    By reducing the memory used for any of the other categories that add up to the total, e.g. Audio, Graphics and any textures etc... This broad a question can not be answered without further details. Some of those details can be obtained via the Memory Profiler, some (like Untracked Memory) may need platform specific Profiling tools to dig deeper for details, until our Memory Profiler is tracking everything.

    Sidenote: Updating to the latest 2019.4.x version may be beneficial for more details when using the memory profiler.
     
  34. IluhaRud

    IluhaRud

    Joined:
    Aug 11, 2018
    Posts:
    1
    Hello there! I have red all of notices in this one, but i really have not seen answer for easy question. How can i get TSMU value in my code? Now i develop simple tool in my project and i want to display parameters from memory profiler on my custom GUI. Please, help me :D
     
    alexeyzakharov likes this.
  35. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    507
    Hi @IluhaRud !
    You can use "System Used Memory" counter (available since 2021.2). E.g. like
    Code (CSharp):
    1. using System.Text;
    2. using Unity.Profiling;
    3. using UnityEngine;
    4.  
    5. public class MemoryStatsScript : MonoBehaviour
    6. {
    7.     string statsText;
    8.     ProfilerRecorder totalReservedMemoryRecorder;
    9.     ProfilerRecorder gcReservedMemoryRecorder;
    10.     ProfilerRecorder systemUsedMemoryRecorder;
    11.  
    12.     void OnEnable()
    13.     {
    14.         totalReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory");
    15.         gcReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Reserved Memory");
    16.         systemUsedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "System Used Memory");
    17.     }
    18.  
    19.     void OnDisable()
    20.     {
    21.         totalReservedMemoryRecorder.Dispose();
    22.         gcReservedMemoryRecorder.Dispose();
    23.         systemUsedMemoryRecorder.Dispose();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         var sb = new StringBuilder(500);
    29.         if (totalReservedMemoryRecorder.Valid)
    30.             sb.AppendLine($"Total Reserved Memory: {totalReservedMemoryRecorder.LastValue}");
    31.         if (gcReservedMemoryRecorder.Valid)
    32.             sb.AppendLine($"GC Reserved Memory: {gcReservedMemoryRecorder.LastValue}");
    33.         if (systemUsedMemoryRecorder.Valid)
    34.             sb.AppendLine($"System Used Memory: {systemUsedMemoryRecorder.LastValue}");
    35.         statsText = sb.ToString();
    36.     }
    37.  
    38.     void OnGUI()
    39.     {
    40.         GUI.TextArea(new Rect(10, 30, 250, 50), statsText);
    41.     }
    42. }
    (more info at https://docs.unity3d.com/2022.2/Documentation/Manual/ProfilerMemory.html)
     
    IluhaRud likes this.