Garbage Collector: System.gc();
May 6th, 2009
Hi again, sorry for the delay to write again. Here im Brazil, 1st May was a holly day and i was play with my son. Well here in agency we have some problens today with system memory of a AIR Project, the project play full HD videos and HD images into a 32″ LCD.
The problem: When i load videos and pictures, so much pictures, inside the project using Loader Class the memory will be always growing and growing and this will close sometime the application.
Solution: Like the post title, Garbage Collector. Well, the Garbage collector is alway actived by Flash Player, when i unload the Loader it will be send to the “trash” of Flash Player, and Garbaga Collector will define when delete the “trash” content, but we can tell to the Garbage Collector that “trash” is Ok to be deleted.
It’s so simple:
1 2 | import flash.system.System; System.gc(); |
If you have a custom loader class you can put the System.gc() into your unload public function to say to the Flash Player that the unloaded content it’s ok to be deleted.
1 2 3 4 5 6 7 | import flash.system.System; ... public function unloadcontent():void { _yourCustomLoader.unLoad(); System.gc(); } |
To force the Flash Player delete the content more speedily you can make a loop with 10 times calling the Garbage Collector:
1 2 3 4 5 6 7 8 9 10 11 | import flash.system.System; ... public function unloadcontent():void { _yourCustomLoader.unLoad(); // Calling System Garbage Collector loop for ( var gci:uint = 0; gci < 10; gci++ ) { System.gc(); } } |
The loop will force the Flash Player delete the content and make your app don’t crash.
It’s really easy, yeah? Try it, comment and if i save your life buy me a beer using the donation block in right side. Thanks for reading again





May 7th, 2009 at 10:10
Nice tip Pablo,
But it is not work on Flash Player running in Browse :/
May 7th, 2009 at 10:19
@ramon fritsch: It was tested in Browser and AIR App and work fine, is more efficient inside the browser than AIR. If you use one System.gc() only the Garbage Collector can take more time to erase the trash than if you use loop of gc().
May 23rd, 2009 at 14:39
Nice post u have here Added to my RSS reader
August 30th, 2009 at 18:56
How long did it take you to write this blog.
October 5th, 2009 at 18:42
@ramon fritsch: It was tested in Browser and AIR App and work fine, is more efficient inside the browser than AIR. If you use one System.gc() only the Garbage Collector can take more time to erase the trash than if you use loop of gc().
October 5th, 2009 at 20:44
Yes @DR, i create my own loader class and on Unload function i use one loop of twenty times calling System.gc() and work so much better than call only one time. Work on Flash Player Plug-in and AIR.
November 27th, 2009 at 15:15
I sometimes cut back on cctv systems with too much wiring
November 28th, 2009 at 21:51
That is nice to finally locate a website where the blogger knows what they are talking about.
January 15th, 2010 at 17:16
Impressive blog. My friends and I were just talking about this the other day. Also your post looks great on my old laptop. And thats rare. Keep it up.
March 17th, 2010 at 23:36
Nice looking blog you have here. The theme is awesome, great color combination.