- Home /
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?
Answer by Catlard · Mar 10, 2013 at 06:28 AM
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#.
You can't load a texture from outside of your project (which takes time, and can't happen immediately) without calling a coroutine, right? So you have to either have update be an IEnumerator function, which I don't recommend or you can load the texture in some other function while Update continues to...well...update.
What I meant by yield return is this:
Sometimes in code, you'll need to do things in a certain order, but you don't want to have a series of Coroutines calling each other. It gets disorganized and spaghetti-ish, in my opinion. So, ins$$anonymous$$d, I use the command:
yield return StartCoroutine(CoroutineA());
FunctionB();
This design pattern ensures that Function B will NOT be called until after CoroutineA is finished completely. Here's an example.
private IEnumerator Get$$anonymous$$ediaImagesFor$$anonymous$$ediaInfo(string imageHalfPath) {
_textureCurrentlyWaitingFor = new Texture2D(0,0);
string absoluteImageHalfPath = imageHalfPath.Insert(0, "file://");
if(File.Exists(imageHalfPath + "_cover.png")) {
yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_cover.png"));
_currentInfoBeingCreated._coverTexture = _textureCurrentlyWaitingFor;
}
if(File.Exists(imageHalfPath + "_inside.png")) {
yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_inside.jpg"));
_currentInfoBeingCreated._insideTexture = _textureCurrentlyWaitingFor;
}
if(File.Exists(imageHalfPath + "_backcover.png")) {
yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_backcover.jpg"));
_currentInfoBeingCreated._backCoverTexture = _textureCurrentlyWaitingFor;
}
if(File.Exists(imageHalfPath + "_preview.png")) {
yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_preview.png"));
_currentInfoBeingCreated._previewImage = _textureCurrentlyWaitingFor;
} else if(_currentInfoBeingCreated._pagePaths != null && File.Exists(imageHalfPath + _currentInfoBeingCreated._pagePaths[0])) {
yield return StartCoroutine(GetTexture2DFromPath("file://" + _currentInfoBeingCreated._pagePaths[0]));
_currentInfoBeingCreated._previewImage = _textureCurrentlyWaitingFor;
}
}
private IEnumerator GetTexture2DFromPath(string path) {
WWW www = new WWW(path);
yield return www;
_textureCurrentlyWaitingFor = www.texture;
}
In this example, each of the texture2ds that I am loading are loaded first, in order, but only then assigned AFTER I know the Texture2D has finished importing. Get it?
When you say "This design pattern ensures that Function B will NOT be called until after CoroutineA is finished completely. Here's an example."
Really? Or do you mean rather that CoroutineA will run until the next yield, before function B will run.
Also, this coroutine stuff seems ancillary to my immediate need which is every frame I need to load a texture, before anything else happens (and yes it needs to finish loading before anything else happens, particularly before or after rendering, but not during). But the problem I'm encountering is one of memory not being released. Can you give me help with this issue?
Thanks.