I ran into memory leak issue when using UnityWebRequest to download texture. I have narrowed down the issue to the following code:
public class WebTest : MonoBehaviour
{
public string url;
public void DownloadImage()
{
StartCoroutine(ImageCoroutine());
}
IEnumerator ImageCoroutine()
{
using UnityWebRequest imageDownload = UnityWebRequestTexture.GetTexture(url);
yield return imageDownload.SendWebRequest();
}
}
I didn’t do anything with the texture, this is to demonstrate the memory leak.
I put the script in a new urp project and call DownloadImage from a button. The memory increases with every click and based on profiler the texture is stored in Not Saved/Texture2D. It persists in editor play mode, development build and shipped build.
The code is almost a copy-paste from the official doc for UnityWebRequestTexture.GetTexture by the way.