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.

Bug Memory spike when loading avatar asset in build only - app dead

Discussion in 'Addressables' started by dan_soqqle, Nov 13, 2022.

  1. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    Been struggling these last few days with a memory spike while loading an avatar through addressables. Can't seem to find out what is causing the memory spike. It used to be OK then suddenly it died.

    The app is perfectly fine on player mode. The spike only seems to happen on build / actual device. Its primarily killing the app on IOS. It also spikes on Android but i think Android has a higher threshold. It later drops back down.

    It's a crazy spike upon loading of an avatar assets. There is nothing fancy about the asset, just a shirt and pants, small textures (< 100 kb). I tried to load the avatar directly on hierarchy and it works fine. It is only broken if i try to load it through addressables.

    More screenshots below. I'm trying to reload my library folder (taking a long time) and reverting to an older build (also taking a long time). will update.

    Any tips to help me troubleshoot?

    upload_2022-11-13_23-23-39.png

    Memory profiler dosen't seem to say much. According to the profiler there is > 1gb but in the map its not much.
    upload_2022-11-13_23-25-7.png

    Memory usage only shows a few hundred. Where is the missing 1GB of RAM?
    upload_2022-11-13_23-25-37.png
     
  2. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    359
    It seems you're using a old version of Memory Profiler. You can use latest MemoryProfiler(>= 1.0.0) to open the MemoryCaptures files generated by the old version. The latest version shows more details that may be helpful for you to investigate this issue.
     
    MartinTilo and dan_soqqle like this.
  3. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    Oh i just saw that it was only available on 2022. I'm still on 2021. I guess i'd be trying to migrate asap.

    However i noticed that a bunch of my memory was coming from shaders that seem to hit FallbackError. Checking it out.

    upload_2022-11-17_12-10-44.png
     
  4. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    359
    To use latest MemoryProfiler, the project dosen't need to be migrated to Unity 2022. For exapmle, for MemoryCaptures generated for our project which is using Unity 2020.3 and MemoryProfiler 0.7, I can use MemoryProfiler 1.0.0 to open them without any problem. Just create an empty project using Unity 2022 and install the latest MemoryProfiler in it.

    It seems the shader 'Universal Render Pipeline/Lit' has multiple copies. This can happen when it's included implicitly in multiple AssetBundles.
     
    MartinTilo likes this.
  5. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    good idea! for the memory profiler. will try that

    Oh i see. i am using addressables, so perhaps the URP is being duplicated. let me investigate!

    May i also ask how you could tell from that picture that it is being repeated?
     
  6. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    359
    upload_2022-11-17_15-14-35.png

    There are three shaders of 'Universal Render Pipeline/Lit', so I think there are three copies in the memory.
     
  7. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    oh i see. and they are referenced by different objects. perhaps that could be a way for me to spot and identify why. thanks!!
     
    Alan-Liu likes this.
  8. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    Hi Alan again sorry,

    can you help me understand the part about included implicitly in multiple bundles? I'm using addressables to manage the scenes. So my scenes are added directly. I have a separate group for the URP shader.lit so i was hoping that prevents duplication.

    However i am quite unclear how assets end up as implicit. Thanks in advance.
     
  9. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    359
    This should prevent duplication. Can you use Build Layout Report to confirm if it duplicates or not actually?
     
  10. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    i just did, and i dont see the lit being duplicated. I did some other testing here:

    1 scene in addressable group - 1 material used on avatar for URP/Lit - 170.9 MB RAM
    2 scenes in addressable group - 1 material used on avatar for URP/Lit - 372.1 MB RAM
    3 scenes in addressable group - 1 material used on avatar for URP/Lit - 459.8 RAM

    I am not sure if its due to the scene having variants, but i am not sure if its linked to addressables.

    Any clues?

    upload_2022-11-22_19-18-52.png

    upload_2022-11-22_19-19-21.png
    upload_2022-11-22_19-19-35.png

    Some info on my shader

    upload_2022-11-22_19-24-30.png
     
  11. JelteS

    JelteS

    Joined:
    Jan 30, 2018
    Posts:
    16
    Hey,

    In the memory profile every shader variant will show as a separate entry.
    So as long as you have the shader in addressables, your bundles should not contain the shader multiple times.

    I think what you should check it to see if you can get a more bespoke shader for the hair.

    Also best would be to split your scenes in separate asset groups, this way they actually unload. Remember, assetbundles only unload when all assets in the bundles have unloaded.

    Additional, after unloading scenes you could do Resources.UnloadUnusedAssets(); and GC.Collect();
     
  12. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    Actually
    Thanks for your comments! Actually in all the cases i shared, only one was loaded, the rest just caused a memory spike as long as the scenes were in the addressable groups.

    The flow of my app is, 1. login -> 2. load player avatar in a game lobby -> 3. choose scene to join.

    And actually i think its not just the hair . . . when i joined an actual scene, the memory spike went exponential that i can't even capture memory profiler (Because it crashed). In this example, I just isolated to one material so i can actually capture something . . .
     
  13. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    359
    From the screenshots above, I saw there are 2 Universal Render Pipeline/Lit shaders, but one of them is only 6.6MB. This is different from the one before, which showed 3 URP/Lit shaders and the growth of the memory of URP/Lit shader doesn't seem to be caused by duplication.

    Here are some methods you can try:
    UPDATE:
    Based on what you posted just now, the third method may not be helpful for this issue.
     
    Last edited: Nov 22, 2022
    dan_soqqle likes this.
  14. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    112
    A bit of update.. i'm still stuck with this after so many days. It seems that its not really so much about the duplicates, but i found the offending scenes. It appears that when i do mesh combining or other adjustments in the scenes (i try to merge meshes, or combine textures) it messes up the mesh and for some reason triggers lots of variants (???). I need to do this because the original scene i have has many triangles and i use some tools to decimate, and combine meshes.

    Overall, according to this post here, it appears that the variants are all loaded in memory thus causing issues.

    https://forum.unity.com/threads/addressable-and-shader-variants.1113805/#post-8314413

    For example, i managed to somehow reset, and it starts with 35mb but after some adjustments (a lot actually, i do for entire scene), then it crashes in build. However, resetting is quite hard, i try to clean, delete the groups, and even revert in Git, and yet the shader is stuck..

    And it takes a lot of time to do builds. =/ Still investigating more.

    If anyone has more information to share about addressables & shaders, or merging / combining of meshes causing shader variants, feel free to share. I tried to look at the IPreprocess and other compilation but its currently quite unclear to me. Will have to spend more time there.