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

Related Posts

10 Responses to “Garbage Collector: System.gc();”

  1. Ramon Fritsch Says:

    Nice tip Pablo,

    But it is not work on Flash Player running in Browse :/

  2. Pablo Davi Says:

    @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().

  3. Pamelavody Says:

    Nice post u have here Added to my RSS reader

  4. Mr. Capone-E Says:

    How long did it take you to write this blog.

  5. DR Says:

    @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().

  6. Pablo Davi Says:

    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.

  7. Viola Says:

    I sometimes cut back on cctv systems with too much wiring

  8. Cool Blog Says:

    That is nice to finally locate a website where the blogger knows what they are talking about.

  9. Ben Teigen Says:

    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.

  10. Laila Ishihara Says:

    Nice looking blog you have here. The theme is awesome, great color combination.

Leave a Reply