as3: does array splice delete an object completely?
-
07-02-2021 - |
Question
I have a lot of display objects in an array that I'm constantly adding and removing from stage. When removed they are not used anymore.
Considering the displayObject is not on the display list, and has no event listeners... will it be garbage collected if I use splice to remove it from the array?
Is it safer if I null the object first?
myArray[2] = null;
myArray.splice(2,1);
Solution
As long as there are no remaining references to the DisplayObject
then yes, removing it from the array using splice
, or even just setting it to null
will allow it to become a candidate for garbage collection.
Update: Setting the item to null
before removal from the array is redundant and will make no difference.
OTHER TIPS
If you need better performance I suggest you using pools instead of creating lots of objects and putting them in array. Use some linked list implementation instead of splice() operation, which is really slow.