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 How can I reduce materials category in memory profiler?

Discussion in 'Editor & General Support' started by Speedy3700, Aug 18, 2023.

  1. Speedy3700

    Speedy3700

    Joined:
    May 23, 2021
    Posts:
    19
    I uploaded my WebGL game to unity play the other day and... it crashed after about a minute.

    Running my computer's activity monitor, I found that the app quickly reached the 2 GB limit for memory.

    Back in unity I ran the memory profiler, and found that 1.27 GB of memory is being taken up by materials.

    It says that there are 526,060 objects, which doesn't make too much sense as this is a relatively small scene. In memory profiler, nearly all of the recorded objects are called "[material name] (instance)." They are all worth 2.5 KB of memory, but this adds up with over 500,000 recorded objects. Some objects do switch materials at some points, so I don't know if this could have an impact.

    I only have about 60 materials in total, and around 1000 objects in the scene in total.

    If you know any reason why this happens, please let me know!

    Thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Accessing a Material from the .material field of a Renderer for any purpose makes an instance of it.

    See the docs on the base Renderer class for more information and alternatives.

    Steps to success:

    - stop making so many Materials.

    :)
     
    CodeSmile likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Replace material(s) properties with sharedMaterial(s).
     
  4. Speedy3700

    Speedy3700

    Joined:
    May 23, 2021
    Posts:
    19
    Thank you so much! This turned out to be the culprit. I had references to a material property in update for about 100 objects, which is a pretty bad idea. Fixed it, and now it runs smoothly.
     
    MartinTilo and Kurt-Dekker like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You're welcome... I still remember when I discovered it in 2012... I had written my code in some way that created one Material per frame per object... it went fine... and then... it didn't.