Reputation: 6282
I noticed something dangerous while working with AS3 - some objects (namely DisplayObjects / MovieClips) tend to remain in the memory and even perform actions after all references to them have been removed.
I work with Flash CS 5.5
and Flash Builder 4.6
.
After getting rid of all references (including removeChild(...) from the Document Class) some objects just seem to stay alive.
This question might come off as a bit broad - but what I am trying to understand is what can I do to MAKE SURE that an object is picked up by the Garbage Collector?
More so, how can I get some sort of feedback from the GC that signifies that a certain object has been destroyed? (For example, a console message)
Thanks in advance.
Upvotes: 1
Views: 130
Reputation: 3851
@weltraumpirat certainly has some good ideas and creating the init/destroy cycle as a working practice is a good one to adopt. There are other issues though that you might want to consider.
Creating new objects can be CPU expensive so using a blanket init/destroy method might not be the best way to go, depending on what your app actually does. You might want to consider creating a pool of objects and recycling them throughout your app.
This is a great article and tool to enable you to track objects in your app to see which objects still have references to them at any point in time.
http://divillysausages.com/blog/tracking_memory_leaks_in_as3
Upvotes: 2
Reputation: 22604
Removing the object from the stage, removing event listeners and setting all references to null
should be enough - GC will pick it up when it is next executed.
It is tedious to keep track of all references, however (and remember this includes event listeners, unless you've set useWeakReference
to true), if you don't have a standard way of doing it... It is advisable to have a "destructor" function in every class, which takes care of cleaning up all variables and event listeners when an object is no longer needed.
You can combine these with events, too - I've come to think that it is good practice to execute your init()
function on Event.ADDED_TO_STAGE
and the destroy()
function on Event.REMOVED_FROM_STAGE. That way you have a clearly defined life cycle for stage objects, and if you make sure your destroy()
method stops all animations and sounds, removes all children and event listeners and sets all member variable fields to null
, you can be quite sure there's no longer any activity going on that could produce unwanted side effects.
By the way - if you actually notice any activity by an object you have removed, some references must certainly still be intact - how else would you be able to tell?
Still, there's no way to know when exactly GC will pick up a discarded object - this is managed by the VM, and you're not going to get a message when it is done. And it shouldn't be your responsibility to think about when to free memory - after all, that's exactly what garbage collectors were invented for!
If you make it a habit to clean up after yourself, your programs will unavoidably become more stable, consume less memory and ultimately perform better. That's all that should matter.
Upvotes: 3