gpt4 book ai didi

actionscript-3 - 检测/记录 AS3 "stop the world"GC 暂停

转载 作者:行者123 更新时间:2023-12-03 14:30:26 24 4
gpt4 key购买 nike

上下文 : 一个大型 AS3 应用程序,它可能会遭受频繁但不可预测的“停止世界”垃圾收集暂停。当一个人确实罢工时,可能需要 30 秒或更长时间才能得出结论。

这在测试中不会发生,但在生产中可能会发生。

问题 :Flash VM 中是否有可用于检测和记录此类事件的日志记录?我在这里借鉴了我在 Java 方面的经验。我已广泛阅读有关 Flash GC 机制的功能(使用标记/扫描进行引用计数),但我正在寻找来自野外运行的应用程序的一些真实遥测数据,因为我知道标记/扫描 GC 事件可以“阻止世界”。

最佳答案

你是对的。垃圾收集器暂停应用程序执行。

The Flash Runtime garbage collector algorithm runs incrementally while marking memory in use. It pauses application execution when collecting unused portions of memory. The pause that occurs as the incremental collection cycle finishes can be longer than desired and can be observable or audible in some programs.


来自 reference
检查垃圾收集器是否运行
据我所知,没有直接的方法可以知道 GC 是否运行。但是可以在 ENTER_FRAME 上执行测试功能。并检查自上次函数调用以来是否已收集垃圾。从最后一帧开始有效。
通过 Dictionary它可以存储弱引用的键,可以查看是否已收集对象。如果是这种情况,垃圾收集必须已经运行。在下面的类中,我创建了一个新对象,以便稍后检查它是否已被收集。
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;
}
}
}
}
通过检查每一帧,您将知道 gc stroke
addEventListener(Event.ENTER_FRAME, onEnterFrame);

private function onEnterFrame(event:Event):void
{
var gcRan:Boolean = GCTest.didGCRun();
}
内存使用情况
您还可以通过检查内存使用情况来监控垃圾收集。文档建议使用 System.freeMemory()

The amount of memory (in bytes) that is allocated to Adobe Flash Player or Adobe AIR and that is not in use. This unused portion of allocated memory (System.totalMemory) fluctuates as garbage collection takes place. Use this property to monitor garbage collection.


我会将此值与 System.totalMemoryNumber() 结合使用
验证执行时间
结合其他方法,记录实际帧速率或代码块的执行时间可能会有所帮助。这可以通过将程序“正常运行时间”存储在一个变量中并在稍后进行比较来实现。
使用 getTimer()

For a Flash runtime processing ActionScript 3.0, this method returns the number of milliseconds that have elapsed since the Flash runtime virtual machine for ActionScript 3.0 (AVM2) started.

var startTime:int = getTimer();
// execution or frame change
var executionTime:int = getTimer() - startTime;
在每一帧上使用时,您可以将其与 stage.frameRate 进行比较并检查差异。
建议垃圾收集器执行
减轻您的停顿的一种可能性可能是建议垃圾收集器手动执行。 System.pauseForGCIfCollectionImminent(imminence:Number = 0.75) 如果实际紧迫性高于参数值,将暂停程序执行。

Imminence is defined as how far through marking the collector believes it is, and therefore how close it is to triggering a collection pause. The imminence argument to this function is a threshold: the garbage collector will be invoked only if the actual imminence exceeds the threshold value. Otherwise, this call returns immediately without taking action.

By calling this function with a low imminence value, the application indicates that it is willing to accept that a relatively large amount of marking must be completed. A high imminence value, on the other hand, indicates that the application should be paused only if marking is nearly complete. Typically, pauses are longer in the former case than in the latter.

imminence:Number (default = 0.75) — A number between 0 and 1, where 0 means less imminent and 1 means most imminent. Values less than 0 default to 0.25. Values greater than 1.0 default to 1.0. NaN defaults to 0.75


来自 reference

关于actionscript-3 - 检测/记录 AS3 "stop the world"GC 暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25513189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com