Memory usage in Flash / Flex / AS3

Posted by ggambett on Stack Overflow See other posts from Stack Overflow or by ggambett
Published on 2010-06-01T20:38:55Z Indexed on 2010/06/02 4:03 UTC
Read the original article Hit count: 302

I'm having some trouble with memory management in a flash app. Memory usage grows quite a bit, and I've tracked it down to the way I load assets.

I embed several raster images in a class Embedded, like this

[Embed(source="/home/gabriel/text_hard.jpg")]
public static var ASSET_text_hard_DOT_jpg : Class;

I then instance the assets this way

var pClass : Class = Embedded[sResource] as Class;
return new pClass() as Bitmap;

At this point, memory usage goes up, which is perfectly normal. However, nulling all the references to the object doesn't free the memory.

Based on this behavior, looks like the flash player is creating an instance of the class the first time I request it, but never ever releases it - not without references, calling System.gc(), doing the double LocalConnection trick, or calling dispose() on the BitmapData objects.

Of course, this is very undesirable - memory usage would grow until everything in the SWFs is instanced, regardless of whether I stopped using some asset long ago.

Is my analysis correct? Can anything be done to fix this?

© Stack Overflow or respective owner

Related posts about flex

Related posts about flash