Search Unity

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

Question Do I need to destroy material that I created with new Material(shader)?

Discussion in 'General Graphics' started by kodra_dev, Feb 20, 2023.

  1. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    106
    I have a simple custom bloom effect. To render it I use:

    Code (CSharp):
    1.  
    2. Shader shader = Shader.Find("CustomSRP/PostFX/Bloom");
    3. Assert.IsNotNull(shader, "Shader not found: CustomSRP/PostFX/Bloom");
    4. _bloomMaterial = new Material(shader);
    5.  
    6. ...
    7.  
    8. buffer.DrawMesh(mesh, Matrix4x4.identity, _bloomMaterial, 0, -1);
    9.  
    My problem is, do I need to destroy the material manually? I know when I access renderer.material I need to destroy it manually. But in this case there is no renderer.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,306
    There's often some confusion about destroying materials. Here's the full quote from the documentation:
    The most important part is you're responsible for destroying it when the game object is being destroyed. This is true for all materials created via script, regardless of there's a "renderer" or not, like for post process effects, but you also generally don't destroy those kinds of components after they've been created unless the level is unloaded. And the level is usually only unloaded when loading a new one, in which case those materials will get destroyed automatically.
     
    warthos3399 likes this.
  3. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    106
    Thank your reply. But I still don't get it.

    How tho? The material is created in a C# script for render pipeline. Not a MonoBehaviour script. It's not held by a game object in any way. Its reference is held by my render pipeline, and I'm pretty sure UnityEngine.Rendering.RenderPipeline is not a GameObject.

    How does Unity magically know it should or shouldn't destroy it when a new level is loaded?

    Edit:

    The document of UnloadUnusedAssets:

    So I guess if it's not held by a game object it'll still get unloaded... I see
     
  4. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    939
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,306
    To give a more direct answer: No, you don't need to worry about destroying it manually.

    You only really need to care about manually destroying materials when it's something you'll be creating and destroying frequently as the material won't be automatically destroyed when the referencing game object is. If you're creating and destroying something that's creating a material every time and don't destroy the material manually it can become a significant memory leak as those materials will just continue to build up in the background using memory.

    Your case is a material that's being created once and effectively kept around for the entire duration of the game existing. I would not expect this to get cleaned up automatically on accident, or for you to have to worry about cleaning it up manually when the object it's attached to gets destroyed. (And it is attached to an object still.)

    If it does get destroyed when it shouldn't, you'll probably notice that pretty quickly because it'll stop working or start throwing errors, and you can add code to remake it again.

    If it doesn't ever get destroyed, unless the code that creates the material is getting called frequently, a single material doesn't actually use that much memory so it's unlikely to be a real issue either.
     
    Last edited: Feb 22, 2023
    kodra_dev likes this.
  6. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    106
    Thank you very much for the detailed reply! Much appreciated.
     
  7. wannaMakeAGame

    wannaMakeAGame

    Joined:
    Dec 7, 2020
    Posts:
    48
    Hey, in my case I create materials an editor window for preview purposes, will it be destroyed when the editor is closed, I believe it will be destroyed as long as I don't save it as an asset?
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,306
    Correct. Though for editor window assets you may want to make sure you're reusing or destroying materials when you can because otherwise a lot can get created before a user ever closes the editor.