0
votes

How can you delete unused and uninitialized variables? I have some masks and filters that may get used depending on screen size, but when I don't need them, can I simply get rid of them? For example:

var appMask:Shape;
if ((screenR % 1) > 0) {
    appMask = new Shape;
    appMask.graphics.beginFill(0x000000);
    appMask.graphics.drawRect(0,0,screenW,screenH);
    appMask.graphics.endfill();
} else {
    //delete appMask variable?
}

I have also considered making the new Shape on variable creation as well and setting it to null for garbage cleanup later, but I want to ensure this will work as expected.

Thanks!

2

2 Answers

3
votes

Short answer, no.

In the example you provided, you didn't actually instantiate any Objects, so you don't really have anything to worry about. The variable will exist for the duration of the scope it was created in, and then it will be gone, with no ill effects. There is no significant memory overhead, and no potentially leaked memory.

In the case that you do instantiate an object, you can't actually delete it. In order to help the automated garbage collector pick it up as quickly as possible, remove all event listeners associated with an object, change (null works) all references to that object, and it should take care of it on its next sweep.

0
votes

In this case you don't have to do anything to delete appMask, if it is not used and not referenced anywhere in the app the memory it occupies will be freed by the garbage collector. You don't have to explicitly set it to null in the else block.

On the other hand you can delete members of dynamic objects using the delete keyword but not local variables.