HTML and XML Garbage Collection in Adobe AIR

Oct 30 2010

Let’s say you want to build an RSS reader in AIR, and you need full HTML support to display the feed. Your RSS reader might switch between a few feeds so you are also loading the XML dynamically.

You’ll notice two things:

  • As the app runs, your memory usage keeps growing and growing.
  • Some RSS entries are too big to be displayed entirely.

This is because:

  • XML is not being garbage collected.
  • The HTML component’s height can only be 2880. (or 4096 in AIR2)

A solution for the HTML height limitation challenge is to dynamically populate a container with multiple HTML components using actionScript (like, in a VBox).

The XML that I was using had a tag which contained HTML. I was fortunate enough to be able to parse the HTML-inside-the-XML and divide it into several chunks by using ”
” as a delimiter:

private function createHTML():void
{
	//rss is the XML from the feed
	//currentItem is the current entry within the feed being displayed
	var content:String = String(rss.item[currentItem].content);
	//by putting the delimiter in parenthesis ( ), it is appended to the array
	var array:Array = content.split(/()/);
	var html:HTML;
		for (var i:uint; i < array.length; i ++)
	{
		html = new HTML();
		html.width = this.width;
		html.htmlText = array[i];
		this.addChild(html);
	}
}

When you change the XML feed or change currentItem, you’ll need to remove the the HTML components (firstly setting their content to null), and also the XML feed. Thanks to Greg Wilson’s Ramblings on garbage collection.

private function clearHTML():void
{
	var html:HTML;
	for (var i:uint; i < this.numChildren; i++)
	{
		if (this.getChildAt(i) is HTML)
		{
			html = HTML(this.getChildAt(i));
			html.htmlText = ""; //set the content to null
		}
	}
	this.removeAllChildren();
}

Before loading any other xml feeds, do this first:

System.disposeXML(rss);

I don’t advise passing the XML down to nested components, because of the way AIR prepares XML for garbage collection (as in, it doesn’t).
Instead, convert what you need to a string or array or arrayCollection, and pass that down to nested components. I do this right away and System.disposeXML(rss) immediately afterwards. For this same reason, I would avoid binding for a project like this. MXML and binding are super fast ways to develop, but in this case, the garbage collection is a bigger issue.

After all that, if the app’s memory usage still grows, you can follow Sean Christmann’s advice (from Craftymind) here.

12 responses so far

Cool Flex Skin

Oct 30 2010

I use flexLib alot. A recent port used .png art assets for the buttons and such. The original target platform was the iPad. Upscaling all the graphics for netbooks and desktops would have been time-consuming, and made the app bigger than necessary. Enter: Enhanced Button Skin. Here’s an example of it’s use:

No responses yet

Emailing from Flex

Oct 30 2010

Here’s how to open the system email client from Flex, and instantiate it with data:

	var urlRequest:URLRequest = new URLRequest("mailto:name@domain.com");
	var urlVariables:URLVariables = new URLVariables();
	urlVariables.subject = "A greeting for you.";
	urlVariables.body = "Hi there.";
	urlRequest.data = urlVariables;
	navigateToURL(urlRequest);

To open a blank email client, leave the mailto: address blank, and omit the URLVariables:

	var urlRequest:URLRequest = new URLRequest("mailto:");
	navigateToURL(urlRequest);

One response so far

Game Development in 2 days.

Sep 25 2010

Over the summer I worked with social media strategist Seth Crofton.  We experimented with a marketing plan to create games in a very short development cycle, like 2 days.

Jedi Macks is based off Matthew Casperson’s Flash Game Development with Flex and Actionscript.  It took us 1 day, Seth creating the assets and me modifying the engine.

Get Adobe Flash player

4 responses so far