As3 and.. Net does not require developers to care about garbage collection. player releases memory for unreferenced objects at an uncertain time. Sometimes, the garbage collector must work as early as possible, to release a large amount of memory occupied Program It runs smoothly, so there is a method called hack on the network, as follows: Function GC (): Void {
Try {
New Localconnection (). Connect ( " Foo " );
New Localconnection (). Connect ( " Foo " );
} Catch (Error: Error ){
// Trace ("GC OK! ");
}
} However, this method does not work very well when the loader object is to be recycled (note that there is a word "too", which still works in the demo below ), then, find that the loader object in cs4 or later has the unloadandstop () method, so that when I remove the loader object from the stage, I will delete it as "clean ", and force garbage collection (call the above GC method ), Code As follows: Pictldr. addeventlistener (event. removed_from_stage, remove );
Function Remove (E: Event ): Void
{
Pictldr. removeeventlistener (event. removed_from_stage, remove );
Pictldr. unloadandstop ();
Pictldr. Unload ();
Pictldr = Null ;
GC ();
} In the demo below, a button is put, and click load and add a SWF file to the stage. A close button is in the upper right corner of the SWF file to remove itself from the stage, I keep clicking the two buttons back and forth. The test results show that the memory will not keep increasing and will remain fluctuating on a horizontal line. The figure shows the truth, that is, the memory usage shown in the task manager during the test:
Attach demo to download
Usage conditions of this demo:
1. I am using cs5, so please open it with cs5;
2. Flash player10.1 r52 is required for the player. I have used flash player10.0 for this test;
3. please go to beloaded. FLA puts more pictures or something. Let's put it around 10 MB. I uploaded only one picture (to reduce space and traffic, huh, huh). It's too small to see memory changes.