Skip to content

Instantly share code, notes, and snippets.

@andyli
Created October 18, 2010 18:22
Show Gist options
  • Save andyli/632717 to your computer and use it in GitHub Desktop.
Save andyli/632717 to your computer and use it in GitHub Desktop.
A Trash that you can throw unused objects inside and see if they are GC'ed later.
//http://gist.github.com/632717
#if flash9
import flash.utils.TypedDictionary;
import flash.net.LocalConnection;
#else
#error
#end
using Lambda;
/*
A Trash that you can throw unused objects inside and see if they are GC'ed later.
http://blog.onthewings.net/2010/10/18/how-to-know-objects-are-really-gced-in-flash-as3/
*/
class Trash {
public function new():Void {
dict = new TypedDictionary<Dynamic,String>(true);
}
/*
Throws a garbage, an unused object that should be GC'ed soon, to the Trash.
An optional identifier that let you know what is in the Trash by garbages().
*/
public function throws(garbage:Dynamic, ?identifier:String):Void {
dict.set(garbage, identifier == null ? Std.string(garbage) : identifier);
}
/*
Return an Array of identifier of the garbages inside.
It does not let you pick out the garbages, since they are dirty, and you shouldn't hold reference of them again.
*/
public function garbages():Array<String> {
return dict.array();
}
/*
Tell if the Trash is empty.
*/
public function isEmpty():Bool {
return dict.empty();
}
/*
Trigger GC.
*/
static public function collectAll():Void {
//flash.system.System.gc(); //it does not collect all the garbages...
// unsupported hack that seems to force a *full* GC
try {
var lc1:LocalConnection = new LocalConnection();
var lc2:LocalConnection = new LocalConnection();
lc1.connect('name');
lc2.connect('name');
} catch (e:Dynamic) { }
}
var dict:TypedDictionary<Dynamic,String>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment