Search Unity

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

Question Actually Memory used

Discussion in 'Editor & General Support' started by Rachan, Nov 29, 2023.

  1. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    747
    Hi there!

    I trying to get actually memory used of my game, and I found this script in this page
    https://docs.unity3d.com/Manual/ProfilerMemory.html

    and I had modified some thing, by convert byte to other unit like this,
    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.     private 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.     private void OnDisable()
    20.     {
    21.         totalReservedMemoryRecorder.Dispose();
    22.         gcReservedMemoryRecorder.Dispose();
    23.         systemUsedMemoryRecorder.Dispose();
    24.     }
    25.  
    26.     private string toUnit(long b)
    27.     {
    28.         float kb = b / 1024;
    29.         float mb = kb / 1024;
    30.  
    31.         string res = "<color=green>" + string.Format("{0:F2}", mb) + "</color> MB.";
    32.         if (mb > 1024)
    33.         {
    34.             float gb = mb / 1024;
    35.             res = "<color=yellow>" + string.Format("{0:F2}", gb) + "</color> GB.";
    36.         }
    37.         return res;
    38.     }
    39.  
    40.  
    41.     private void Update()
    42.     {
    43.         var sb = new StringBuilder(500);
    44.         if (totalReservedMemoryRecorder.Valid)
    45.         {
    46.             sb.AppendLine($"Total Reserved Memory: {toUnit(totalReservedMemoryRecorder.LastValue)}");
    47.         }
    48.         if (gcReservedMemoryRecorder.Valid)
    49.         {
    50.             sb.AppendLine($"GC Reserved Memory: {toUnit(gcReservedMemoryRecorder.LastValue)}");
    51.         }
    52.         if (systemUsedMemoryRecorder.Valid)
    53.         {
    54.             sb.AppendLine($"System Used Memory: {toUnit(systemUsedMemoryRecorder.LastValue)}");
    55.             statsText = sb.ToString();
    56.         }
    57.     }
    58.  
    59.     void OnGUI()
    60.     {
    61.         GUI.Box(new Rect(10, Screen.height / 2, 250, 50), "");
    62.         GUIStyle style = new GUIStyle();
    63.         style.normal.textColor = Color.white;
    64.         style.richText = true;
    65.         GUI.TextArea(new Rect(10, Screen.height / 2, 250, 50), statsText, style);
    66.     }
    67. }
    Now I can see that byte converted to MB. or GB.

    so I start a new project with everything empty,


    But it still show a hundred of MB. of my project!
    I don't know why or I wrong in converting?

    anyone?
    Thanks!!!
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,407
    Those numbers don't look unreasonable... What numbers where you expecting?
    Also what platform and what Unity version are you on and, if you take a memory snapshot of it with the Memory Profiler Package, what does that tell you about how much is used
     
    Rachan likes this.
  3. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    747
    Thanks!
    so if I want to see actually memory using of my app?
    because I still having the memory issue, my game took memory more and more at start it using 400 MB, But after about 20 min, it increase to 500++ MB I don't know where to fix, and I use profiler or memory profiler But I still don't know where to see.
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,407
    I'm unsure what you mean by "actual memory usage" do you mean the total? Allocated or Resident? Or with full details?

    You keep posting not enough info for me to be able to help you. I can't guess what your issue is without way more details.

    If memory usage grows, then as in the other threads: take two snapshots and compare them.
     
    Rachan likes this.
  5. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    747
    Thank you very much for your answers!

    I think I should optimizing as much as possible,
    do you have suggestion? for mobile?

    Thanks!!!
     
  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,407
    Happy to help. And yes I do, e.g. this page is a good one to start on.

    Also, I link to a bunch of resources in my signature here (needs to be the desktop version of the forum, or going to my profile).
     
    Rachan likes this.
  7. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    747
    Thank you very much again, Unity and Supporter are the best!
     
    MartinTilo likes this.