You're right. The garbage collector pauses application execution.
The Flash Runtime garbage collector algorithm runs gradually when memory is used. It pauses application execution while collecting unused portions of memory. The pause that occurs at the end of the incremental collection cycle may be more than desired and may be observable or audible in some programs.
from reference
Make sure the garbage collector has completed
As far as I know, there is no direct way to find out if the GC works. But it is possible to execute a test function on ENTER_FRAME and verify garbage collection since the last function call. Effectively since the last frame.
Through the Dictionary , which can store weak reference keys, you can see if the object was assembled. If so, garbage collection should be started. In the next class, I create a new object to check later if it is assembled.
package { import flash.display.Sprite; import flash.utils.Dictionary; public class GCTest { private static var dict:Dictionary = null; public static function didGCRun():Boolean { if ( dict == null ) { dict = new Dictionary(true); } var hasKeys:Boolean = false; for ( var obj:* in dict ) { hasKeys = true; break; } if ( hasKeys ) { return false; } else { dict[new Sprite()] = null; return true; } } } }
By checking each frame, you will find out if gc stroke
addEventListener(Event.ENTER_FRAME, onEnterFrame); private function onEnterFrame(event:Event):void { var gcRan:Boolean = GCTest.didGCRun(); }
Memory usage
You can also control garbage collection by checking memory usage. The documentation recommends using System.freeMemory()
The amount of memory (in bytes) that is allocated by Adobe Flash Player or Adobe AIR and which is not used. This unused portion of allocated memory (System.totalMemory) fluctuates when garbage collection occurs. Use this property to control garbage collection.
I would use this value in conjunction with System.totalMemoryNumber()
Check lead time
In combination with other methods, it may be useful to record the actual frame rate or runtime of code blocks. This can be achieved by storing the uptime programs in a variable and comparing it at a later point.
Use getTimer()
To process the Flash environment in ActionScript 3.0 Flash, this method returns the number of milliseconds since the start of the Flash Runtime Virtual Machine for ActionScript 3.0 (AVM2).
var startTime:int = getTimer(); // execution or frame change var executionTime:int = getTimer() - startTime;
when used in each frame, you can compare this to stage.frameRate and check for discrepancies.
Advise the garbage collector to perform
The ability to soften your pauses may be to advise the garbage collector to do it manually. System.pauseForGCIfCollectionImminent(imminence:Number = 0.75) pauses program execution if the actual proximity is higher than the value of the arguments.
Imminence is defined as how far a collector can tag, he says, and therefore how close he starts a collection pause. The imminence argument for this function is threshold: the garbage collector will only be called if the actual proximity exceeds a threshold value. Otherwise, this call returns immediately without a decision.
Calling this function with a low probability value, the application indicates that it agrees that a relatively large mark should be completed. On the other hand, a high probability value indicates that the application should be suspended only if the labeling is almost complete. As a rule, there are more pauses in the former case than in the latter.
imminence : Number (default = 0.75) - a number from 0 to 1, where 0 means less inevitable, and 1 means the closest. Values ββless than 0 are 0.25 by default. Values ββgreater than 1.0 by default 1.0. The default NaN is 0.75
from reference