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 Why all project shaders stay in memory all the time?

Discussion in 'Shaders' started by roundyyy, Jul 17, 2022.

  1. roundyyy

    roundyyy

    Joined:
    Dec 23, 2019
    Posts:
    110
    Hello,
    Sorry if it's dumb question. I'm completely confused here. My game has about 15 scenes, I'm using various shaders on them. They are stripped as much as I could without causing many issues, but the problem I'm facing is that shaders in build takes nearly 9GB(!) of RAM (according to profiler on development build). When I profile the same scene in editor it's only 400MB in shaders which is reasonable. It happens on all scenes basically, build 9GB, editor couple hundreds MB. I see on development build profiling that all shaders (from whole project build) are loaded to memory and I'm 100% sure there is no reference in scene for the rest of them. Is that normal behaviour? From what I know not used shaders (not even once before) shouldn't be in memory right?
    so why all shaders are loaded in memory from beginning? I made build that includes only main menu and one of the scenes and I still see all shaders from the rest of the scenes (that are even not included in build). I'm not using variant collections, cause game is still under development and scenes will be still worked on. Unity 2019.4.40
    Am I doing something wrong here?
    Thank you for help!
     
    Last edited: Jul 18, 2022
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,002
    If I remember correctly it’s just an old bad Unity design decision.
     
    roundyyy likes this.
  3. roundyyy

    roundyyy

    Joined:
    Dec 23, 2019
    Posts:
    110
    oh god...
     
  4. roundyyy

    roundyyy

    Joined:
    Dec 23, 2019
    Posts:
    110
    Is there any way to unload them from memory? They just sit there and do nothing, until scene with them is loaded.
     
  5. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    The Editor only ever loads shader variants that are required for rendering. The Player loads all variants that are included in the build for a given shader.

    This shouldn't be the case. Shaders in this sense are regular assets and should be loaded on demand and unloaded as soon as nothing is referencing them.
    Do you have them referenced in "Always included shaders" list by chance?
     
  6. roundyyy

    roundyyy

    Joined:
    Dec 23, 2019
    Posts:
    110
    Hi, thanks for your reply.
    No, I don't have them in "Always included shaders". When I load certain scene I see they sit in memory and referencing to prefabs and materials that are not definitely in that scene, which is weird imo
     
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    I would suggest reporting this as a bug - relevant people will take a look.
     
  8. roundyyy

    roundyyy

    Joined:
    Dec 23, 2019
    Posts:
    110
    Thank you, was bit worried that it's a bug cause it didn't make any sense for me
     
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    It may also be something incorrect in your setup, but it's difficult to day without looking at it.
     
  10. roundyyy

    roundyyy

    Joined:
    Dec 23, 2019
    Posts:
    110
    of course I'm aware of that, still investigating, I reduced number of shaders significantly, no needed keywords, variants etc. so got reduction to about 4GB for shaders in runtime, which I can consider acceptable. Still I see shaders from other scenes, but it's bit better now. Reported bug, but cannot add project to support for reproduction of issue cause it's huge.
     
  11. roundyyy

    roundyyy

    Joined:
    Dec 23, 2019
    Posts:
    110
    I contacted support, linked request to this thread, but have no reply since then (it's more than 3 weeks). It's pretty annoying, cause shader memory usage is couple gigs (about 4-5), shaders and variants that are not in scene sits in memory, In editor is couple MBs usage. I have stripped a lot of variants, but just cannot strip more without causing issues (I already broke lightmapping for custom mods in game)
    CASE IN-10573
    @aleksandrk
    Thank you
     
  12. SharpAccent

    SharpAccent

    Joined:
    Jul 19, 2018
    Posts:
    39
    Did you ever find a fix for this?