Hey all,
Can somebody confirm this for me. If you use a Texture2D.GetPixels(mipmap) call and don’t have a SetPixels to go along with it, the memory will not be released… here’s the code I have just as a quick test.
If you have a 1024 x 1024 texture in there within a few minutes your system should come to a crawl… and your ram usage will go to about 1.5 GB with an empty scene with 1 object with this script attached.
Am I missing something?
Why wouldn’t the local color arrays be dropped from RAM?
Any suggestions on how to prevent this?
var theTexture : Texture2D;
var theTexture2 : Texture2D;
function DoIt ( aTexture : Texture2D, texture2 : Texture2D ) {
var thePixels : Color[] = aTexture.GetPixels(0);
var thePixels2 : Color[] = texture2.GetPixels(0);
aTexture.SetPixels(thePixels, 0);
aTexture.Apply();
}
function Update() {
DoIt (theTexture, theTexture2);
}
Okay, so I was wrong it does it with Texture2D.SetPixels too. I left Safari open for about ten minutes. It was fine and then the RAM usage just took off like a rocket. I have it to be able to run in the background too, so that isn’t the reason for the sudden increase… hmmm?
var theTexture : Texture2D;
//var theTexture2 : Texture2D;
//function DoIt ( aTexture : Texture2D, texture2 : Texture2D ) {
function DoIt ( aTexture : Texture2D ) {
var thePixels : Color[] = aTexture.GetPixels(0);
// var thePixels2 : Color[] = texture2.GetPixels(0);
aTexture.SetPixels(thePixels, 0);
aTexture.Apply();
}
function Update() {
// DoIt (theTexture, theTexture2);
DoIt (theTexture);
}
I don’t know… I think filling an array of size 1024 x 1024 every frame could do that as well…
From the description, it sounds like it’s getting you an array of the individual pixel colors, and that texture is pretty big. If you’re filling that array once a frame, that could be a pretty large load.
Or, are you saying that the memory isn’t impacted when you setpixels for both of them? It’s not that clear whether you’ve tested that already.
Okay, here’s the new code… it doesn’t seam to leak at all when using only one texture/pixel array, but, when using two like the following code, it leaks like crazy, can I only use one array at a time?
It would be really nice to have a manual garbage collection system available, but, I guess that’s a JScript problem and not a Unity problem… I guess. Or is there one hidden away somewhere that someone knows about.
var theTexture1 : Texture2D;
var theTexture2 : Texture2D;
function DoIt () {
var thePixels1 : Color[] = theTexture1.GetPixels(0);
var thePixels2 : Color[] = theTexture2.GetPixels(0);
theTexture1.SetPixels(thePixels1, 0);
theTexture1.Apply();
theTexture2.SetPixels(thePixels2, 0);
theTexture2.Apply();
}
function Update() {
DoIt ();
}
You can tell the Mono garbage collector to run by calling
System.GC.Collect();
Note, however, that doing this doesn’t always seem to make Unity release that memory back to the OS.
Thanks Jormungandr!
That seams to work really great! Like you said, it seams to miss some occasionally, but, it seams pretty rare, so, it’s no big deal.
Thanks again,
You’re the man,
Nathan