Adobe AIR HTML Control, Flash Content and Garbage Collection

For the past few days, I’ve been trying to figure out why Tour de Flex’s memory and CPU utilization increases every time a new sample is viewed.  Today, I had a breakthrough that explains the bloat and a simple workaround:

Summary:  If an mx:HTML control loads a page with flash content, the control cannot be freed, even if you remove it from the container, de-reference the object, etc.  The flash content continues to play!  However, if you first set the control’s htmlText attribute to an empty string (“”), it will be garbage collected as expected.

Below is my evidence.

Here’s my test code:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="800" height="600">
<mx:Script>
	public function killIt():void {

		//browser.htmlText = "";	commented out for the 1st profiler graph below.	

		this.removeChild(browser);
		browser = new HTML();
		browser.location = "http://www.youtube.com/watch?v=d8Vw5r7-vgs";
		this.addChild(browser);
	}
</mx:Script>
<mx:HTML id="browser" location="http://www.youtube.com/watch?v=d8Vw5r7-vgs" width="100%" height="100%" />
<mx:Button label="Kill the HTML control" click="killIt()"/>
</mx:WindowedApplication>

The code displays a simple HTML control that loads a YouTube page.  When you click the button, the HTML control is removed from the container, recreated and set to the YouTube page again.  The original object is de-referenced and replaced by the new one so the old one should get garbage collected… that’s how I expect it to work anyways.  However, you can hear the original video playing in the background AND the newly loaded video playing at the same time.  If you click the button a few more times, things get a little crazy sounding! (my apologies to Kenny Drew Jr.!).

When I do this with the profiler running, I get the following:

Notice that each time I click the button creating a new HTML control, the memory consumption increases.

However, if I un-comment the line that reads, browser.htmlText = “”, I get very different results.  I only hear the latest YouTube page as expected, but I also see garbage collection doing what I originally expected it to do:

So, my conclusion is that if the HTML control has loaded a page with Flash content, the resources are not released and the Flash content continues despite my previous efforts.  However, if I simply turn off the Flash content by giving it an empty string to load, everything works nicely.

In Tour de Flex, each sample is a simple HTML wrapper with a SWF.  Therefore, if you view 50 samples, all 50 SWFs retain their resources until you quit the AIR application, so you eventually start having issues.  By adding a single line of code, things are much better.   We will be posting a new version of Tour de Flex in a few days with this fix and many other goodies.

If you have seen different results or if you have a better workaround, let me know!

~ by gregorywilson on February 24, 2009.

13 Responses to “Adobe AIR HTML Control, Flash Content and Garbage Collection”

  1. I have had similar issues with Loader object in FP9 (linux version). The loaded video has been disappeared from stage when I had used removeChild, = null, but the sound didnt stop. After a half an hour the sound stopped, but until that I had to mute the loader. Not so pretty but worked, and the GC released the memory in that way too.

  2. By the way, nice workaround!

  3. Thanks for the tip!
    I shared it to french folks there:
    http://www.flex-tutorial.fr/2009/02/25/adobe-air-composant-mxhtml-avec-contenu-swf-et-garbage-collection/

  4. Does setting browser = null produce the same desired results, or is the leak still present?

  5. […] > Adobe AIR HTML Control, Flash Content and Garbage Collection « Greg Wilson’s Ramblings […]

  6. I noticed something similar. I had an HTML control displaying a webpage with a Flash song player. When I closed the containing window, the music kept playing. The only way I could get it to stop was to set the htmlText to “”.

  7. […] Fixed a memory/CPU issue  (see previous post) […]

  8. […] Fixed a memory/CPU issue  (see previous post) […]

  9. […] Fixed a memory/CPU issue  (see Gregs Ramblings previous post) […]

  10. Hi there, If you don’t like topics with many links, just delete this topic.
    Thankyou.

  11. […] the the HTML components (firstly setting their content to null), and also the XML feed.  Thanks to http://gregsramblings.com/2009/02/24/adobe-air-html-control-flash-content-and-garbage-collection/ private function clearHTML():void { comments.clear(); var html:HTML; for (var i:uint; i […]

  12. […] 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 < […]

  13. You are doing a very magnificent job! This is an truly cool article.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: