Describe type is slow, and consume lots of memory, the XML object returned leaks easily due to the master String never deleted if you assign any node to your objects…
so the best way to avoid leaking too much memory is to cache the result to always return the same XML instance for a certain type, it is also much faster !
Solution? Cache the results !
static protected var s_describeTypeDic:Dictionary = new Dictionary(); static public function describeType(o:*):XML { var cacheKey:String; if (o is String) cacheKey = o; else { cacheKey = getQualifiedClassName(o); //Need separate entries for describeType(Foo) and describeType(myFoo) if (o is Class) { cacheKey += "$"; } } if (cacheKey in mDescribeTypeDic) { return mDescribeTypeDic[cacheKey]; } var xml:XML; s_describeTypeDic[cacheKey] = xml = flash.utils.describeType(o); return xml; }
And same goes with the getDefinitionByName() function:
static protected var s_definitionByName:Dictionary = new Dictionary(); static public function getDefinitionByName(name:String):Object { if(name in s_definitionByName) return s_definitionByName[name]; else { var o:Object = flash.utils.getDefinitionByName(name); m_definitionByName[name] = o; return o; } }
Leave a Reply