View Full Version : Removing Unreferenced Objects from Memory
ReleeSquirrel
October 15th, 2009, 01:05 PM
I'm trying to make a Starfield program as practice for ActionScript 3. I'm using a timer event in the document class to create Star objects which are defined by an object class connected to the movieclip Star in the Library. The object class has a timer as well, and at each tick it repositions itself a few points away and scales up, creating the image of a star shooting towards the sides of the stage. When the star has gone past the boundaries of the stage or when it has scaled to a certain size, it removes itself from the display list with the command:
this.parent.removeChild(this);
This removes the star from the display list just fine, however after that I can't get the star to set itself to null so that it will be removed from memory, and I'd like some suggestions on how to do this properly. An idea I had was to make a function in the document class that the child objects can call, something like this.parent.KillMe(this) which would give itself as a reference to the function, which could then use a local variable reference to set the child to null, removing it from memory. This all seems terribly complex, so I'm sure there's another way.
I haven't noticed a memory leak or slowdown, however, so I'm curious if objects without references that are removed from the display list are automatically removed? That could be both good and bad, as the object might want to add itself to the display list again at a later time; not in this Starfield program, but perhaps in another I write.
Iamthejuggler
October 15th, 2009, 01:47 PM
When you remove all references to an object (i.e. remove it from any arrays it's in, off the display list, remove any event listeners related to the object etc) it gets put in a queue of items ready to be garbage collected. You can't force it to be garbage collected, you just have to wait for the flash player to decide it needs to garbage collect, at which point it will be removed from memory. I believe that before the flash player takes more memory from the system it will garbage collect first, so as long as all references to the object are gone you shouldn't have a problem.
ReleeSquirrel
October 15th, 2009, 11:43 PM
Alright then, I'll try to keep that in mind in the future. I had been hoping to use objects that move themselves around independantly, but I'll have to put them in an array or something that references them so that they stay existing if they're taken off the display list.
Thank you very much.
Shaedo
October 16th, 2009, 12:28 AM
To force garbage collection do this
import flash.system.*;
System.gc();
EDIT read below to see how this is not correct (at least for this issue)
dandylion13
October 16th, 2009, 01:00 AM
Whoa...!!! That's a new one for me, Shaedo!
IQAndreas
October 16th, 2009, 05:19 AM
Sadly, it's only available to AIR, exe files, or when running in the debug player.
From the Adobe Language Reference (http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/system/System.html#gc%28%29):
For the Flash Player debugger version and AIR applications only. In an AIR application, the System.gc() method is only enabled in content running in the AIR Debug Launcher (ADL) or, in an installed applcation, in content in the application security sandbox.
So you can use it when running your own apps, but once you release it to the internet, it will no longer garbage collect when that line appears. (I'm not sure if by "Flash player debugger version" they mean debugging with CTRL+SHIFT+ENTER, or if they mean the special debug player that tells error messages, while the normal player is silent.)
senocular
October 16th, 2009, 08:38 AM
(I'm not sure if by "Flash player debugger version" they mean debugging with CTRL+SHIFT+ENTER, or if they mean the special debug player that tells error messages, while the normal player is silent.)
the second one
Scythe
October 17th, 2009, 03:23 AM
This is good to know.
Observation: all 7 replies to this thread were from different users.
Shaedo
October 17th, 2009, 05:21 AM
BIG EDIT
System.gc() does work on the ctrl-shift-enter .swf created. (using CS4 flashplayer 10)
System.gc() does not work on the published .swf. (As stated by IqAndreas)
The code I used was:
package
{
import flash.display.Sprite;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.None;
import flash.system.*;
public class Main extends Sprite
{
public function Main():void
{
create1();
}
private function create1():void
{
var sprite1:Sprite = new Sprite;
sprite1.graphics.beginFill(0xFF0000);
sprite1.graphics.drawRect(0, 0, 100,100);
addChild(sprite1);
var tweenx:Tween = new Tween(sprite1,"x",None.easeOut,0,500,30,true);
var tweeny:Tween = new Tween(sprite1,"y",None.easeOut,0,500,30,true);
System.gc(); // comment me out to see difference
}
}
}
Garbage collection will remove the tween (needs to be in an array or somthing to prevent this).
you can see them published here
http://www.shaedo.com/gcdemo/
where http://www.shaedo.com/gcdemo/systemgcdemo.swf has the
'System.gc()' commented out whilst
http://www.shaedo.com/gcdemo/systemgcdemo2.swf
does not have the System.gc()' commented out.
They behave the same, you will have to run the code your self to see that the System.gc() stops the tween on a ctrl-sht-ent demo.
Its a pity that we can't force GC in published swfs, it would be REALLY REALLY useful!!!
S.
JonnyR
October 17th, 2009, 09:53 AM
Shaedo,
I disagree with your comment that it's a pity we can't force GC in non-debug Flash Player's as it defeats the whole point of having managed code; manual garbage collection shouldn't be a concern (However, the whole GC'ing of remote content is another issue!).
J.
Shaedo
October 18th, 2009, 08:24 AM
@ ReleeSquirrel appologies for the hijack.
@ JonnyR
1st) I respect you opinion (you are one of the people the teach me flash through your own postings) but I don't see why you think its better to have less control. I am not saying they should remove the automated GC, just that having the option to control it if desired would be good. I think ReleeSquirrel provides a good example of somthing that many people encounter where: flash does not run GC in such a way as to provide optimal efficiency. Dont get me wrong I think that Flash does a really excellent job in general.
2nd) technically you canforce GC in non-debug Flash Player's. I (think I) have provided the code so you can see this for your self.
dandylion13
October 18th, 2009, 11:35 AM
Hello,
I actually have no idea how memory is managed by the player. That's good, because (unlike many other programming languages) I don't have to worry coding it.
The only problem is, do we know how well the player actually does this? If it's super-optimized, that's great, but if not... ?
Does anyone out there know the details of the player's memory management? Thanks :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.