Flash, XML and memory management
October 29, 2003

Since I work with Flash and XML fairly intimately, I'm surprised I didn't notice this before...

If you are using the "Processes" tab of your Task Manager (at least in Windows anyway) to track memory management, you may have come across this too. I was just doing some tests by loading a fairly large xml file (about 600k) locally to see what effect that has on memory used by the Flash player. Just doing a simple:
var sampleXML = new XML();
sampleXML.onLoad = function(success) {
   if (success) {
So I watched the memory usage of the standalone Flash player jump from about 4MB to about 9MB while this happened. No huge surprise...the player parses the XML data into native objects which will result in more memory usage. However, I tried to remove the XML from memory using a button that just runs:
trace("sampleXML before delete:" + sampleXML);
delete sampleXML;
trace("sampleXML after delete:" + sampleXML);
When I run this code, everything works as expected:

1) "sampleXML before delete" traces out the XML string.
2) "sampleXML after delete" traces out "undefined".

So far so good.

However, when I go back to check the memory usage, it seems that it's still hovering around 9MB. I do a double take and wonder what's going on. I tried similar tests and all of them seem to result in the same thing...when you delete an XML instance using "delete", it doesn't seem to clear it from memory. I also tried the following ways of clearing it:
// Attempt 1 - replace old instance with new empty instance
sampleXML = new XML();

// Attempt 2 - assign empty string to instance
sampleXML = "";

// Attempt 3 - replace old instance with new instance 
// and load in empty XML file
sampleXML = new XML();
None of these scenarios caused memory to be freed up. So I proceeded to search the usual places...Flashcoders archive, were-here, flashkit. Nothing. Until I stumbled across a post on Flashkit with the following info:

>The memory monitor doesn't necessarily monitor how much memory is currently
>in use by Flash , but rather how much has been *requested.* Flash reserves
>this memory until another process requests it, but >relinquishes it upon
>request. Using the memory monitor may not be giving you >an accurate

>Explicitly deleting an object will cause Flash to release >that chunk of
>memory internally for other uses. In the case of >automatic garbage
>collection (such as when a function with local variables >exits) the memory
>is not immediately released - the instance count is >decremented and the
>garbage collection actually deletes the object when there >are enough free
>cycles to do so. This can appear as a "delayed reaction" >as far as memory
>being freed up.

>Macromedia Tech Support

Has anyone else heard of this before? Am I the only one in the dark?

Is there something else I'm missing here? At work, we deal with large amounts of metadata formatted as XML so memory management as we load and unload this data is crucial. Anyone else have anything to add?

Posted by philter at October 29, 2003 04:30 PM

Comments Disabled