I am writing code for Unity in C#. I found a major error in the way I am writing my scripts, but I am not sure of the correct method I should be using instead. I will often create temporary lists, floats, textures, etc. in my scripts. I also often initialize my variables with new
. I thought this unneeded data would just “disappear” when it was not being used. However, this is not happening as I expected.
I found this while experimenting with some code which can be simplified as:
public class PhotoTestClass {
//public Texture2D photoTexture = new Texture2D(500,500); // USING THIS LINE CAUSES INSANE PROGRESSIVE MEMORY USE IN THE LOOP BELOW
public Texture2D photoTexture = null; //USING THIS LINE CAUSES MILDER PROGRESSIVE MEMORY USE IN THE LOOP BELOW
public PhotoTestClass(Texture2D setPhotoTexture = null) {
if (setPhotoTexture == null) {
setPhotoTexture = new Texture2D(500, 500);
}
photoTexture = setPhotoTexture;
}
}
public class TestClass {
public void TestTextureWork() {
List<PhotoTestClass> photoClassList = new List<PhotoTestClass>();
photoClassList.Add(new PhotoTestClass(Resources.Load<Texture2D>("photo1")));
photoClassList.Add(new PhotoTestClass(Resources.Load<Texture2D>("photo2")));
photoClassList.Add(new PhotoTestClass(Resources.Load<Texture2D>("photo3")));
photoClassList.Add(new PhotoTestClass(Resources.Load<Texture2D>("photo4")));
photoClassList.Add(new PhotoTestClass(Resources.Load<Texture2D>("photo5")));
//would then run some operations on this temporary list and store results to persistent public/private variables
//would like all above data created cleared from memory at end of function
}
}
If I then inside my app run this as a function in my app on demand (eg. triggered by a button):
private void runFunction(){
TestClass tempTestClass = new TestClass();
for (int i = 0; i < 100; i++) {
tempTestClass.TestTextureWork();
}
}
In all circumstances, texture memory incrementally increases after time I run the function and does not come down. However, if I used the line public Texture2D photoTexture = new Texture2D(500,500); in PhotoTestClass, memory usage SKYROCKETS after each run by another several gigabytes each time. The incremental increase with public Texture2D photoTexture = null; is much smaller but still happens.
I tried putting the code inside TestTextureWork() in a while loop with a bool to make it run once and tried photoClassList.Clear() at the end but neither solved it.
What is the reason I am getting so much memory use (especially with new Texture2D(500,500);) and why is texture memory incrementally being used more and more the more times I run this? It seems like there is a lot of data being persistently created in the memory and never cleared. How would I clear all this data as I go?
Basically, what am I doing wrong? Is there a correct way to initialize variables or make temp lists/variables that doesn’t cause persistent memory use? Or how would I manually best clear things at the end in the example above?
Thanks for any help or guidance.