Possible Texture memory leak

I’m having a major memory leak issue possibly related to texture memory

In our program, a large number of textures are loaded into a list at the beginning on the program. The user can select one of these images and it pops out onto a moveable tile that can then be closed. Unfortunately the new tile seems to use more memory and doesn’t release it afterwards.

The function that sets the texture of the new tile is effectively this:

public void initTexture(texture _input)
{
    renderer.material.mainTexture = _input
}

In my main script I call both Resources.UnloadUnusedAssets() and GC.collect

Is there anything I’ve missed? Is there some specific way to unload the texture? I was under the impression Unity only held one copy of each texture, does my tile class create and store a new version of it?

Hi, consider using Texture2D.Destroy / DestroyImediate. It should be working properly.

private void UpdateMaterialMainTexture(Texture2D texture) {
		
		// destroy old if exists
		if (_currentMainTexture != null) {
			Texture2D.DestroyImmediate(_currentMainTexture, true);
		}
		
		// assign a new texture
		_currentMainTexture = texture;		
		
		// set a new material texture
		this.renderer.material.SetTexture("_MainTex", _currentMainTexture);
	}