views:

832

answers:

3

How to free up memory and destroy Array, is it ok to do just like this

  myarr = null
  myarr = new Array

is all the old content of myarr now freed from memory?

+1  A: 

Assuming you haven't got another reference to the array in another variable somewhere the arrays memory will be recovered when the GC gets to it.

AnthonyWJones
A: 

myarr = nothing

Garbage Collection in .Net would sort this anyway. Thats if your in .NET.

JamesM
he's using Flex.. (GC as well)
Assaf Lavie
+1  A: 

If there are no more references to that Array Object, then it is collected by the Garbage Collection (GC) system. So when you ask "is all the old content of myarr now freed from memory?", the answer is it could be. But you have no control over when the GC runs using the Actionscript Virtual Machine.

So it is true to say, when there are no more references to an object, the next time the Garbage Collector runs, it will be collected.

Also things get more complex when you are dealing with a Array. The Array object might be collected, but that does not mean that the objects the Array references will be cleaned up too. The objects the Array references might be one of many references to those objects, for instance maybe a copy of that Array exists in your system, meaning at least two references exist to each object in each Array.

Also be aware that event listeners stop objects from being Garbage Collected, unless they are weak event listeners

Brian Heylin