My app is gradually increasing in memory usage but after a number of minutes (around 10 minutes) of non-use the memory usage goes down to a more reasonable level. I am wondering if this is the behavior of Adobe's built in garbage collector and if there is anyway I can help to encourage the app/flash to shed off the memory quicker.
Here is a brief explanation of the app. The app has a number of categories. Each category has a playlist of thumbnail images. When you navigate to a category, the thumbnail images are downloaded. Each thumbnail image's bitmapdata is copied resized to fit the app's layout and the original bitmapdata and loader are disposed of. When the user goes to a new category, the resized bitmapdata is disposed of and then the next set of thumbnail images are downloaded and resized. (Also note: that the loader of any thumbnail images that were in the process of being downloaded from the previous category are closed.)
When the app first launches, the size of the app is around 12MB. During my testing of the app, I navigate to one category and then to 2-3 other categories. I select the same category multiple times. Looking at the flash profiler, I can see that the memory usage peaks and valleys - but in an inconsistent manner. Sometimes it peaks and then valleys after selecting a new category. Other times it just peaks but does not valley after selecting a new category. Plus selecting the same category over and over may result in different peak/valley behavior. Eventually after about 30+ category changes, the memory usage peaks to about 30MB. It could go higher (or slightly lower) after selecting more categories after reaching that 30MB peak.
If I do not do anything and just let the app alone, eventually the app's memory usage falls to a more reasonable rate of about 13-14MB. There isn't a timer in my app that releases any memory/objects so I am unsure why there's a huge release of memory after time. I also noticed that once it reaches a certain level of memory usage, then it is easier for the memory usage to peak to that level even though it may valley to a considerable amount below that peak. I did a comparison between a valley and peak, and 98% of the loitering objects are bitmapdata objects.
What may be going on here? Why does the memory get released after some time of non-use? Is there a way I can encourage flash to release that memory much sooner?
thanks!
I seem to remember that the Flash garbage collector could be a little erratic, you could try setting the variable that holds the downloaded thumbnails to null after you're done with it?
So if you're holding them in an array you could loop through and set each value to null e.g.
for(var i:uint = 0; i < yourArray.length; i++)
{
yourArray[i] = null;
}
Not sure if that will help but it's worth a try.
Thanks. I've already tried that. I think I tried all the tricks. Settings things to null. Deleting references of the bitmapdata objects if they are contained in associatibve arrays, etc.
For example,
delete _oAssociativeArray[sID];
I would have thought that I missed to null a variable that is holding a bitmapdata/loader if it weren't for the fact that the memory usuage goes dramatically down after non-use. If a variable still contained the loader/bitmapdata, then I would think the variable would still be around when the memory usage goes dramatically down.
I have gone thru the code and it looks like I am properly preparing the objects, but it is possible I may have missed something.
Why are the objects ready in about 10 minutes after the app is not being used?
The app does nothing for about 10 minutes between reaching a peak and then dipping to a more reasonable memory usuage. No objects are being prepared for gc during those 10 minutes.
Is it something built into Flash's GC to eliminate after 10 minutes objects that weren't properly prepared? If so, what is the easiest way to find these unprepared objects so I can prepare them and so they are eliminated sooner than 10 minutes of inactivity?
thanks!
gc doesn't occur at timed intervals.
when an object is created, if new memory is needed, the player checks all existing (in memory) objects and counts the number of references to that object. if an object has 0 references that object is marked for removal on the next frame tick. at least, that's the way it is supposed to work.
but i've found the actual behavior of the player can be quite a bit different, with regards to the above and everything else i've checked, than advertised. and the exact behavior depends on the flash player version.
with flash pro, you have to create your own memory checker to see what's in memory at any given time. flash builder or flex may have better tools (than flash) for memory management.
North America
Europe, Middle East and Africa
Asia Pacific