texture.LoadImage doesn't release memory

Calling texture.LoadImage(imageData) causes memory to be consumed and it doesn’t get released. Eventually Unity crashes. Calling these functions doesn’t help:
System.GC.Collect();
System.GC.WaitForPendingFinalizers();

Any ideas?

1 Answer

1

This is the function I use to load images – I’ve never had a crash because of it. After the yield return StartCoroutine from this function, I use the Texture2D stored in the _textureCurrentlyWaitingFor variable. Does that help?

private IEnumerator GetTexture2DFromPath(string path) {
	WWW www = new WWW(path);
	yield return www;
	_textureCurrentlyWaitingFor = www.texture;
}

For a non-game simulation purpose, I need to load the texture in Update(). When I replace _textureCurrentlyWaitingFor with renderer.material.mainTexture it doesn't crash but the texture isn't loading. This is different behavior. With my other code the texture loaded. I'm not completely getting the 'yield return' part??? Also, an example showing how to call this function in Update() would help, as I'm pretty new to C#.

–