Adobe Air Garbage Collection On Local Images
I'm currently working on an Adobe Air based screen saver (technology choosen for ease of communicating to a central server). Things were going along well until I started to do performance testing today and I realized that garbage collection was not freeing up what I expected it to be.
To sum up my app very briefly I have a Canvas component that wraps an Image componant in order to give the apearance of a poloroid photo. When I get past a certain threshold I remove these wrappers via a removeChildAt(0) to sluff off the oldest image, freeing up space and memory. Turns out nothing was actualy being cleaned up. I found some good resources on garbage collecting that informed me of a few things I didn't know (specific to flash) but nothing got me closer...
Then the light upstairs randomly turned on and I thought, well if I there isn't anything on the outside of the wrapper holding on to it (such as an event listener), maybe there is something on the inside. This might seem like an obvious place to look, but my wrapper was only a handful of lines long and did absolutly nothing but look pretty... However due to the fact that I'm pulling images from a server down to the local disk (which is all done way prior to trying to draw to screen), my image source looks like this: new File(imagesDir.nativePath).resolvePath(aPhoto.name).nativePath.
SOLUTION:
In order to get garbage collection to work, I wrote a quick function inside my wrapper:
public function compact():void {
(this.getChildAt(0) as Image).source = "";
this.removeAllChildren();
}
I call this function first, then poof.... all my garbage collection works as "expected". Not only am I suprised that this worked, I'm suprised that I had to do this. Why on earth wouldn't an image get removed if it's container was destoyed?
Also, I ran tests without the this.removeAllChildren(); while garbage collection still worked, the overall average memory usage went up almost 650k for some reason.
Relate Reading:
About garbage collection
Why is the memory usage of my Flex app not coming down?
Garbage Collection and Memory Leaks
luca mezzalira wrote on 12/24/08 6:40 PM