I came across an interesting problem the other day with Flex (SDK version 3.3) and embedded images. I was trying to be smart and minimize memory usage by creating a new instance of the BitmapAsset class from an embedded image, and then releasing the memory by setting the reference to null. As it turns out that doesn't work. See the example below. There is one embedded image, and a new instance of that image is created when the button is clicked. There is nothing referencing the new instance though, so you would expect that the garbage collector would eventually clean it up. But if you watch the memory usage of this application it goes up every time the button is pressed (using a large embedded image makes this more obvious), and it never goes down.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Image;
[Embed(source="image.png")]
public var EmbeddedImage:Class;
]]>
</mx:Script>
<mx:Button x="10" y="10" label="Button" click="{new EmbeddedImage();}"/>
</mx:Application>
The only workaround I found was to make a static instance of the embedded image and reference that one instance throughout the life of the application.
Written
by
Matthew Casperson
(4,877
pts
)
in
Matthew Casperson Blog
Last Edited
on
Jul 2 2009, 09:59 AM