0
votes

I am working in on a Flex / Air application (using PureMVC) written virtually entirely in ActionScript 3 (i.e. we use very little of Flex). View components are added to a Canvas object in the main application window and these Views then load other movie clips, images files, etc.

When I remove a View from the stage, I do so using application.canvas.removeChild(...). This fires an event in my PureMVC mediator for that View, which removes any event listeners that were set up and everything appears to be dandy.

However, the movie clips are still playing in the background. They are not causing any trouble on screen ... but they are simply there, playing somewhere in memory (e.g. I can see trace statements from them) and I want them gone. I suspect the View that was removed from the Canvas is still there too, but I can't prove it.

Setting the View to null doesn't do anything. I'm surprised that AS3 doesn't include any method to simply and effectively destroy a Display Object and all of its children.

Is the only way to do this to tediously unload all of the movie clips, etc. and then hope that Flash will clean them up?

2

2 Answers

2
votes

AS3 is a garbage-collected language, so there is no explicit command to remove objects from memory. However, the AS3 VM does do reference counting, so if you remove a display object from the stage and remove all references to that object, the object will get destroyed immediately or very soon after, so you will no longer see trace statements from it, etc.

If you have removed all the apparent references to the object but it doesn't get collected, the problem is probably circular references. For example parent and child objects reference each other, so the above handling will not know to remove them unless you unchild everything. In these cases the objects will not get removed from memory until a garbage collection (i.e. mark-sweep) occurs. (GCs are relatively slow so the player internals optimize when they occur based on how much memory is available and other factors.)

Incidentally, all of the above is true of all objects, regardless of whether they are display objects or Flex Views or NetStreams or whatever. For further information, here's a good overview of the AS3 GC - it's old but still correct.

But what you should understand about all this as a programmer is that in nearly all cases, you shouldn't need to worry about this. Remove all your references to objects (and stop() them if they're playing), and let the (heavily optimized) internal GC decide when they actually disappear. If memory gets scarce then the GC will act, and if it doesn't then any additional memory management would simply be premature optimization.

0
votes

There are two things here

  1. if you are trying to remove movie clips dynamically generated by your application then a simple removeChild or removeChildAt would effectively remove it from stage. Setting the variable to null is in practice .
  2. if the movie clips are externally loaded swf' with audio/video data then you would have to unload all of them in proper sequence . stop-> unload -> remove loader from stage ->destroy variables ( as needed).

Depending on the version of flash player you have various unloadAndStop type methods and methods of soundmixer class stopAll.

Cleanup would come into picture only when you are exiting your entire application.