- Home /
cleanup allocated memory for texture2D
I'm wondering if you create a new Texture2D and fill it up with something and then later recreate them and fill them up again, how do you release the memory allocated the previous time? So if for example you want to keep the same name but recreate the texture with new dimensions. I'm writing a rather lengthy routine but this is a problem i have, as my application eventually crashes. If i create the Texture2D only once this problem doesn't occur. So i can only conclude that if you construct a new Texture2D... memory is allocated at some point but if you then construct it again with other dimensions somehow there's a need to cleanup the memory previously allocated but i don't see a clear way to do this.
Does this make any sense, and if so what can be done about it?
Answer by Slobdell · Jan 23, 2014 at 11:20 PM
What language are you using? In C# you rely on garbage collection to free up memory and it does it when it feels like it. Best you can do is make sure all references to it have been destroyed. I have a feeling, however that something you're doing is extremely inefficient if that's your problem but unless you post code and explain more it's impossible to know.
I'm using C# and i actually did find out a way to free allocated memory by setting texture to null and then use UnloadUnusedAssets and garbage collection to clear it all out. This actually did solve it to the point that it can be run without issues in Unity Editor but an iOS build for iPad will still eventually crash though it will take a while longer for that to happen now. The complete function is over 260 lines long so i'm not going to post it, but i went over it many many times and i'm pretty sure there's nothing inefficient about it. I've also found another solution using different scenes to solve it fortunately.
I did not end up destroying the references in my previous method and i suspect that is whats causing problems. By references you mean for example if the texture is assigned to the material of a GameObject, that this GameObject should also be destroyed correct?
Yeah. The object doesn't have to be destroyed but the reference should be set to null.
String example = "some words";
String another = example;
Now there are two references to the same memory location. When garbage collector comes around it will see there are still references to that spot and leave it be. In order for that memory to be freed you need example = null; another = null;
A lot of times it's easy to be doing work with one and completely forget about the other and it's just a reference that sits around and clogs up memory.
Yeah finally figured it all out, as it happens i still needed to take care of this. So for anyone else that might be reading this, in a coroutine don't forget to yield UnloadUnusedAssets(); because if you don't its very likely the unloading will be incomplete.
not very difficult:
AsyncOperation op = Resources.UnloadUnusedAssets();
while (!op.isDone) yield return 0;
GC.Collect();
I tried as you suggest It stays the same, I also have www.textures running in a coroutine. The memory goes up every time I try a different fix which unloads assets and things.
I have started a new question on the topic: http://answers.unity3d.com/questions/1112217/texture2d-wwwtexture-memory-leak-not-fixed.html
Your answer
Follow this Question
Related Questions
Kinect SensorData Memory Leak 1 Answer
ManagedStaticReferences when unload unused resources 0 Answers
Detect WWW Image bitmap dimensions? 2 Answers
Is this memory usage too much for a mobile game? 1 Answer
Memory allocation issue 1 Answer