During the testing phase of a current project we had many reports of virtual memory errors. Faaantastic. So I load up Firefox and click around the site while watching my memory using Vista's task manager. Not much is happening. I click around to different pages. I click on every video. I try to click on everything as quick as possible. I try to sit through every video. Nothing. No spikes. Sure it jumps around here and there but nothing significant. My gut instinct is A. How much crap do these people have open on their computer? and B. How old are their computers?
Although those may have been valid questions, I was completely off. Turns out with some more digging that every person who had a problem was using IE. So I load up IE. I don't even have to click on anything and I can watch the memory climb all the way up to 1gig before it drops down to 100mb and starts its ascent again. Holy crap, 1 gig???
The culprit was easy enough to find... we have a reflection on the home page that is getting updated onEnterFrame. If the memory is going up and I haven't even clicked on anything, that has to be it. For the reflection we had 1 bitmapData that was getting disposed every frame and then set to a new BitmapData object. I didn't code this, but at first glance it seemed to make sense. The dispose should get rid of the data and then a new one is created. I knew from Moock's book that the Garbage collector works on its own timetable, but I had no clue that it would allow the Player to reach 1 gig before it ran. I also had no clue that it would behave differently in IE than it would in FF.
It was easy enough to fix. I noticed the width/height of the bitmapData was never changing so there was no need to dispose the old one and create a new one. We just needed to create one bitmapData in the constructor and then call the draw function onEnterFrame instead. Memory leak or not, this was much more efficient than destroying and creating a new object on every frame. This kept IE sitting at a steady 70mb instead of the whopping 1gig.
After this exercise I decided to brush up on my garbage collector knowledge. I highly recommend checking out Grant Skinner's articles: Understanding garbage collection as well as Resource management strategies in Flash Player 9.
One gem I took away was that you need to remove the enterFrame listener on any sprite that you are removing, EVEN if you did not add a listener yourself... I didn't realize that the built in listener could cause the object to stay in memory.
Anyways, it was a good exercise and I thought I would share. Morals of the story:
1. Test on multiple browsers and environments. You can't assume Flash will behave the same in each.
2. Don't create new objects when you can just alter an existing one.
3. A full understanding of how garbage collection works is very important and worthwhile to research.