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 to fix RAM increasing from creating textures

Discussion in 'Scripting' started by sildeflask, Oct 8, 2023.

  1. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    155
    I have a method that uses readpixels to create a texture and assign it to a material
    I am using MaterialPropertyBlock.SetTexture to set it
    each time I run the method the ram increases by 10Mb
    Even though I am always overwriting the same property block

    I think all these textures arent getting unloaded/Destroyed?

    Everytime I run the method I dont need the old textures, only the new one

    Can someone give me some advice on how to proceed?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,379
    What are you doing with the old texture?

    Texture's are created in unmanaged code. They must be destroyed to be cleared out of memory.
     
  3. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    155
    nothing at all, i iwill investigate this problem thankyou
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    It's also possible that if you feed a texture into this endpoint, perhaps Unity makes a copy.

    If so I would expect that your previous texture injections would simply be lying around without any references and therefore be eligible for UnloadUnusedAssets() cleanup, which of course fires automagically when you change scenes.

    If you call UnloadUnusedAssets() after setting your new texture, does the leak still occur?
     
  5. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    155
    I will try this aswell, thanks