Array - Difference between .Clear() and Null?
Hello,
I have a Texture2D array (using the built in arrays). This array is populated with generated textures during runtime.
In terms of memory clean up, what would be better to use:
myArray.Clear();
or
myArray = null;
Would .Clear still keep the images referenced somewhere (thus won't be able to remove from memory when Destroying). Is there any difference here?
Thanks.
C# is a real production language. That means for C# Qs like this, Googling will give you pages and pages of explanations of what Clear does (and lots of lots of comments from people, with votes, so the information tends to be more reliable.)
What P$$anonymous$$U wrote below seems fine, but there are lots of C# Q's here in Unity Answers where the single answer is pasted from the worst answer on a real C# site. It's faster and better for you to Google.
Answer by phil_me_up · Jan 18, 2016 at 01:42 PM
Clear() will mark the list as empty, but wont actually resize the array to 0, so a very large list might hold on to some memory. When using Clear(), the references to objects in that array are removed so the garbage collector will handle memory for you (assuming nothing else if referencing those textures).
I will tend to use Clear if I am likely to want to repopulate that list at some point. If it's a single use list and I'm never going to use it again (rare), then I would consider setting to null.
Basically, the garbage collector will handle things for you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612060641im_/https://answers.unity.com/themes/thub/images/avi.jpg)