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. Dismiss Notice

Question Is assets-ram profiling supported in unity 2018?

Discussion in 'Editor & General Support' started by lee2022, Jan 17, 2023.

  1. lee2022

    lee2022

    Joined:
    Feb 9, 2022
    Posts:
    20
    I wanna know how much ram is taken for textures、meshse、audios ...
    I know afterward Unity 2020.2, we have editor api like ProfilerRecorder, but in Unity 2018 how can I do it?
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,096
    You can
    • Use the Profiler APIs(see note at the top about availability in non-development players) to query some of those high level counters, such as Profiler.GetTotalAllocatedMemoryLong().
    • There is no runtime API for e.g. just the amount of memory taken by Textures or Meshes. There is an ugly workaround though, that is essentially the same as what the Memory Profiler Module does under the hood in native, but in managed code, with GC.Alloc overhead:
      • var texturesObject.GetAllObjectsOfType<Texture>();
        then for loop over that with
      • total += Profiler.GetRuntimeMemorySizeLong(objects);
        [*]This only works in development builds and can be slow and as mentioned, has a bit of a memory overhead itself y not least the creation of that array of objects. So if you need that sort of information I'd recommend:


    • Capture memory snapshots using MemoryProfiler.TakeSnapshot()

      • If an Editor is connected, the snapshot will be streamed over, otherwise you'll get a local file that you'll need to extract yourself
      • Pre 2019, take snapshot didn't stream the managed memory but created a copy to write out, meaning that if your close to crashing OOM, you might want to capture with just the CaptureFlag.NativeObjects | CaptureFlag.NativeAllocations
      • You can import snapshots from older Unity versions into Editors with a newer version and the Memory Profiler package installed, e.g. 2022.2 and package version 1.0.
     
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,096
    Somehow the formatting broke a bit. Can't seem to be able to fix that properly...

    Anyways, with all of these goes: we've done a lot of Bugfixes and improvements to the Memory Profiler backend and data over the years. 2018.4 did not receive all of these, so e.g. the sizes some of those assets take up in memory could be reported somewhat incorrectly.
     
  4. lee2022

    lee2022

    Joined:
    Feb 9, 2022
    Posts:
    20
    Thanks, one more question:
    Can I monitor GC ( like : how long does it take, how often does it happen ...) on mobile (not in editor) ?
     
    Last edited: Jan 31, 2023
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,096
    In 2020.2 you'd have the ProfilerRecorder API for that, but in 2018 you do have the Recorder API and I believe that GC.Collect should be a profiler marker that's available for recording in release builds. But you can check that via
    isValid