Object Graphs, the GC, and System.disposeXML()

AIR 1.5.2 contains a new, performance-related API that heavy users of XML may find handy, System.disposeXML(), that is an instructive example regarding use (and abuse) of the AIR garbage collector.

When an XML document is loaded, it’s stored in memory as a graph of individual objects representing elements, text, attributes, and so on. Each pair of related objects–for example, a parent/child element parent–are linked, in the underlying representation, with pointers going in both directions.

This is a convenient representation for traversal, but can be problematic for the garbage collector. As I mentioned in my earlier post about referencing counting and ActionScript objects, the garbage collector will eventually find and discard the entire set of objects after the last external reference is removed. However, it takes more work for the GC than less-connected data structures.

In practice, the amount of time it takes the garbage collector to do its job is related both to the number of allocated objects and the number of pointers between them. So while it is nice that the GC is capable of finding even these highly-connected graphs of objects, using them is also asking it do a lot of work. You can reduce this workload by explicitly disconnecting the pointers between objects.

For graphs of ActionScript objects, you can ease the GC workload by simply nulling out reference between the objects. Note that you don’t have to find every last reference for this to be helpful. The GC will still do it’s job in the end, and every bit of clearing references helps. For ActionScript objects this can be particularly helpful because, if their reference count then goes to zero, they can be cleaned up even before the GC sweep completes.

XML objects are a trickier case because the API doesn’t allow developers to traverse the actual object graph. That’s where System.disposeXML() comes in: It traverses the internal object graph backing the XML object, setting all of the internal points to null. Afterwords, the XML object is empty and, presumably, useless. But then, since you were done with it anyway, that shouldn’t be a problem.

6 Responses to Object Graphs, the GC, and System.disposeXML()

  1. Juan Delgado says:

    Hi Oliver, thanks for the explanation!

    Can you guys please add a link in both System.disposeXML() and the XML docs to this post? Otherwise it’s difficult to understand why such a method is even necessary!



    • Oliver Goldman says:

      I’ve added a link from the comment section in the documentation. It’s up to the tech writers to decide whether they want to make a change to the document itself.

  2. Vitalik says:

    Hi i had question to disposeXML() method it’s require
    Language Version: ActionScript 3.0
    Runtime Versions: Flash Player 10.1, AIR 1.5.2
    When i try it’s in air app it’s works,
    but when to try it on web using Flash player 10.1.84 and try on 3.2 and 4.0 sdks it’s can’t be compiled no Flash Builder 4 and therefore i can’t use it’s. Can you explain what i do wrong and simple sample how to do right.

    • Oliver Goldman says:

      Make sure you’re targeting SWF version 10. Older Flex SDKs default to targeting SWF version 9, I believe.

      • Vitalik says:

        I set target player to 10.0.0

        But still get error on compiling

        “Call to a possibly undefined method disposeXML through a reference with static type ”

        Maybe some changes must be performed on Flash Builder 4, can you help me to resolve this problem, because to free memory is important thing in WEB development

    • vergil castelo says:

      I have the same problem in a web application, in which I load in a ton of XML. I’m using SDK 4.1 and targeting the latest Flash Player… ?