Garbage collection in Flash Player 8
While this was not a feature I worked on I still want to point it out. Flash Player 8 contains a new garbage collection (GC) system for ActionScript. Due to the nature of the beast this is somehow a feature which does not get too much Ahhs and Ohhs. It is one of those important under the hood things you simply never think about or should have to think about. But it is one of the most challenging and complicated feature an engineer can work on, it took a whole release. It is simply amazing to watch an engineer look at some raw dump of memory and tell me what type of object it contains. I am sure that when he does day to day life mental arithmetic he uses hexadecimal instead of decimal. :-)
Flash Player 7 and lower used reference counting to keep track of ActionScript objects. That means that each object created in ActionScript had a tracking counter attached to it. If that counter became zero the object would be released and memory was freed. Every object means not only objects like MovieClips, Arrays etc, but also simple types like Numbers, Strings and Booleans. This is a very simple system and worked well in the past. With the advent of large applications done in Flash this model has extreme drawbacks: High memory usage since even simple types need a reference count, low performance since incrementing and decrementing the reference count takes time and the potential for circular references with the result that memory might never be freed (although weak references somewhat reduced this issue).
Flash Player 8 features a mark and sweep GC, very much like most of the implementations of the Java run time. While I could try explaining all the details (probably with little success due to my limited involvement), there is an excellent Wikipedia article on this subject if you are interested. One of the drawbacks of the mark and sweep model is that the collection of objects is not immediate but deferred until a later time. This means that it could block the player for an undetermined amount of time if there is a lot to collect. Early versions of the Java run time had this problem if you remember, sometimes everything would freeze up for many seconds. To get around this problem Flash Player 8 implements an incremental GC. With the new GC in place you should see higher ActionScript performance and less peak memory usage. I can't give you exact number since it depends on the application, but in some very special cases we have seen memory usage decrease of 40%.
The true challenge of this project was to take the existing reference counting system and move it over to the mark and sweep model in the existing code base without breaking old content. I do not know if something like this had ever been attempted before, but I amazed how well it works now. I am also really excited that the Flash Player is growing up and getting closer to other run times out there like Java.
Flash Player 7 and lower used reference counting to keep track of ActionScript objects. That means that each object created in ActionScript had a tracking counter attached to it. If that counter became zero the object would be released and memory was freed. Every object means not only objects like MovieClips, Arrays etc, but also simple types like Numbers, Strings and Booleans. This is a very simple system and worked well in the past. With the advent of large applications done in Flash this model has extreme drawbacks: High memory usage since even simple types need a reference count, low performance since incrementing and decrementing the reference count takes time and the potential for circular references with the result that memory might never be freed (although weak references somewhat reduced this issue).
Flash Player 8 features a mark and sweep GC, very much like most of the implementations of the Java run time. While I could try explaining all the details (probably with little success due to my limited involvement), there is an excellent Wikipedia article on this subject if you are interested. One of the drawbacks of the mark and sweep model is that the collection of objects is not immediate but deferred until a later time. This means that it could block the player for an undetermined amount of time if there is a lot to collect. Early versions of the Java run time had this problem if you remember, sometimes everything would freeze up for many seconds. To get around this problem Flash Player 8 implements an incremental GC. With the new GC in place you should see higher ActionScript performance and less peak memory usage. I can't give you exact number since it depends on the application, but in some very special cases we have seen memory usage decrease of 40%.
The true challenge of this project was to take the existing reference counting system and move it over to the mark and sweep model in the existing code base without breaking old content. I do not know if something like this had ever been attempted before, but I amazed how well it works now. I am also really excited that the Flash Player is growing up and getting closer to other run times out there like Java.


28 Comments:
thank you tinic for another great article! stef
GC is a huge topic, and you're right about it not getting oohs and ahhs... but it certainly should.
An improved GC is really huge as people are building much more complex applications with Flash. Add cacheAsBitmap and other things, and Flash can flex a lot more muscle. I love it!
Thanks for the article Tinic!
Dude,
I for one thank who ever it was who took time and effort to fixing the GC. I posted on this problem a while back and i began to loose faith in the Flash Platform much like i did with DHTML and its GC issues.
The only thing left for you to do is figure out a way to allow UI to be serialized inside Local Shared Objects. As if you guys want to call this a "Platform" fit for Enterprise, it needs this one missing piece and then we can start "Flexing" our muscles and have "Intranets" built out of FLASH soley.
It just doesn't scale yet, and with this we think it may.
Tinic, nice info to have. GC is one of those things that is probably working if you never have to worry about it. That being said, is there anything we should be doing differently to ensure a smooth and efficient clean up with the new system?
Does it help the GC process if I clear out references with something like themovie_mc = null as soon as I am sure the reference will no longer be used ?
Tinic, just wanted to thank you for all your postings. Very informative and enjoyable to read about the inner workings of Flash.
Hi Tinic,
I´ve posted this message on flashcoders and nobody seems to have an answer.
Could you please comment on this?
While porting some components so they can take advantage of the new methods of FP8, i saw the following:
If you enable the redraw regions, and use scrollRect somewhere, you will see that the player redraws the complete clipped
movieclip, instead of the clipped region only.
What are the benefits if the player has to redraw the whole screen? (assuming you clipped content has the same size as your
stage, and of course the clipped area is smaller).
Thanks in advance!
Hey Tinic,
Thanks for the info! It's like watching the behind the scenes stuff on a DVD!
-erik
Hum a awsome feature, and cool to do that like java do.
Tinic a strict other question I ask to you.
When the E4x will be included on the Flash Player?
mail me igorcosta@igorcosta.com
Marcelo: This is a known bug we haven't been able to fix in this release. It has now become much more visibile since you can display redraw regions. I hope we can tackle this next time around.
Dominique: It does somewhat, although the object will not be freed right away like in FP7. Make sure you reuse objects when you can instead of create excessive amounts of them. In essence the same GC performance tricks as in Java apply.
BIT-101: One example: Particle systems. If you have objects representing particles, reuse the objects when they die instead of allocating new ones. Allocating large amounts of objects in a short time can kill the GC since unlike with the referencing counter model object are not freed right away anymore. So when objects die, put them into a recycle container from which you create more particles.
again very valuable info Tinic, thanks heaps for that :)
linked to it on the game dev board as people are always hunting for low mem usage and high performance there ;)
http://www.flashkit.com/board/showthread.php?p=3402117#post3402117
I don't know if that helps but results are interesting :
Flash 8 Garbage Collector benchmark
interesting stuff tek, i´ll just wait for Tinic´s post before posting any assumptions.
One thing though,in your comparison image it says "FP7 memory usage" on both lines,you should change one to fp8 =)
>"FP7 memory usage" on both lines
Corrected, thanx ;)
I think it could be a good thing to do some benchmark to demonstrate how the new GC improve usage of memory when you re-use the same object to store data as Tinic says because I have seen great improvements during my tests.
There will be a more official article on DevNet this week which will show some benchmarks we used internally.
I can not really get into details of the GC and why some memory is not fully released (my guess it has something to do how we memory map address space through the OS). I'll try to ask the engineer in charge next week.
Thanx for this precision. I will await new information from DevNet impatiently.
> BIT-101: One example: Particle systems. If
> you have objects representing particles,
> reuse the objects when they die instead of
> allocating new ones.
Flash Player should actually do that! IMHO the developer should not have to think of such complex memory management.
Tinic, don't forget to tell memory management developers that "escape(<2 MB string>)" still takes 11 seconds on FP8! (comsumed in a simple 'rep stosd' asm ins)
I think the benchmarks that Tinic was talking about are there.
This post has been removed by a blog administrator.
Are there any hooks into the GC so you can call it at after a large memory consuming operation explicitly or even when you know there will be some idle time? Like C# provides?
I have a big problem with memory on Flash 7.
I have a big project with many FLV videos.
On some computers, sometimes, sounds desapear for a while on the video.
It seems that it's a memory problem. Sometimes, a FLV starts with no sound, but in most cases, it works.
There are a lot of FLV playing before (15-16) and then, 2 or 3 next does not have sound. It's very chaotic, sometimes the sound is here and sometimes not on the smae video. On most computer, all work weel but on computers with integrated sound card, I have these problems.
I tried so many things like reusing variables, deleting objects, but I have still no sound on some videos.
Do you know where I must search to find a way to fix this bug?
Sorry for my poor english, I'm french...
Cedric
I had a game that was working in FP7 but now after just a few characters are killed the browser completely crashes, no alerts or anything. I was using an MVC for the game, and there was large amounts of garbage collection. I notice the game crashes more easily on safari than firefox. And only when items are removed from the stage. This seems very suspicious in a GC sort of way. Does this seem like an issue with GC to you?
I found some sort of bug in CG system (dont know if it is really problem of CG cause it work on ActionScript stuf) When ya drag big objects (limits of object are zoomed out of current viewable area) FlashPlayer seems to eat system memory in many of MBs, and never release it. I used my memory (1.5 GB) in 3 minutes.
I found a bug posting a file in Flash Player 8. I am using an interval, and for some reason Flash Player keeps adding 4K to the system memory every 1 second. So... although they say it's improved, I only see an improvement when (a) publishing to Flash Player 8 (b) embedding in Flash Player 9 and (c) running the movie in Flash Player 9.
I like articles like this. Thanks!
I am facing a major issue while using Flash Player 8 and Actionscript 2.0.
Size of IExplore in which I play my SWF file increases by 4K every few seconds constantly and after a specific time interval IE stops responding.
Can anyone suggest me the steps to take to clean up memory so that my IE does not have such memory leaks?
I am using things like dynamic movieclips, onEnterFrame, classes, etc.
Thanks for all the help!
Post a Comment
<< Home