在要 GC 的对象中将引用设置为 null?

发布于 2024-10-24 18:45:55 字数 626 浏览 0 评论 0原文

// in a garbage collected VM, destroy someObject:
someObject.a = null;
someObject.b = null;
someObject = null;

我听说在 Java 或 C# 等良好的 VM 中,您不应该这样做。将 someObjectab 设置为 null 会减慢垃圾回收速度,因为 GC 需要更长的时间才能发现之前引用的对象 ab 不再被引用,而如果您保持它们不变,GC 将在清理 someObject.

假设我听到的是真的(如果不是,请纠正我),AVM2、ActionScript 3 VM(特别是在最新版本的 Flash Player 中)是否相同?

我问这个问题的原因是我有一个同事这样做,因为他从以前的雇主那里了解到它更快,而且 Flash 有很多这样的怪癖(我觉得很容易相信)。

我只是想知道该信息是否仍然是最新的(对于其他类似的 Flash 优化也是如此)。根据我的经验,类似的优化技巧在生活平台中很快就会过时。

// in a garbage collected VM, destroy someObject:
someObject.a = null;
someObject.b = null;
someObject = null;

I have heard that in a good VM like Java's or C#'s, you shouldn't do this. Setting someObject's a and b to null will slow down garbage collection because the GC takes longer to find out that the objects a and b previously referred to are no longer referenced, while if you leave them intact the GC will immediately check upon them when cleaning up someObject.

Assuming what I heard is true (correct me if not), is it the same for AVM2, the ActionScript 3 VM (specifically in the latest versions of Flash Player)?

The reason I ask is I have a colleague who does it like that, because he has learned at a previous employer that it's faster, and that Flash has many quircks like that (which I find easy to believe).

I'm just wondering if that information is still up to date (for other Flash optimizations like that as well). In my experience optimization tricks like that get outdated rather quickly in a living platform.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-10-31 18:45:55

我无法向您提供任何具体的硬数据,以确定哪个设置更快 - 设置为 null 或不设置为 null。老实说我不相信有人会。

我能做的就是为您提供一些有关垃圾收集的信息,您可以从中做出决定。

从 Flash Player 9 到 Flash Player 10 Adob​​e 做出了一些重大改进,特别是在内存管理方面。有几个“加载外部 swf”内存错误、未引用的声音残留等。其中大部分已得到纠正。

Grant Skinner 重点介绍了 GC 用于确定应删除哪些对象的技术。请参阅这个精彩的演示(带有一些很酷的交互),了解其工作原理 http://gskinner.com/talks/资源管理/(但请注意该演示是关于 FP9 的)

正如他所说,有引用计数和标记清除。 AS3 中的 GC 最终归结为引用。如果一个对象(非原始类型)有对它的引用,则不会被删除,但如果没有,即无法访问它,它将被标记为删除。但您无法控制删除实际发生的时间 - 可能是这一帧,也可能是下一帧。

最终,在我看来,“清空”每个变量/属性(引用或其他)是浪费且非常难以管理的。对于较小的东西可能很实用,但是当对象变得更大并包含 100 个变量时,您无法一致地将所有变量都归零。

沿着这条轨道,Flash Player 只会以正确的方式优化其 GC,而不是相反。

I can't provide you any specific, hard data on which is quicker - setting to null or not. I honestly don't believe anyone will.

The best I can do is provide you with some information around garbage collection, and you can make your decision from there.

From Flash Player 9 to Flash Player 10 Adobe has made some serious improvements, especially around memory mangement. There were several 'loading external swf's' memory bugs, unreferenced sounds sticking around, etc. Most of which have been rectified.

Grant Skinner has highlighted the techniques the GC use to establish what objects should be deleted. See this excellent presentation (with some cool interactions) on how it works http://gskinner.com/talks/resource-management/ (but do note the presentation is about FP9)

As he states there is Reference Counting and Mark Sweeping. Ultimately GC in AS3 comes down to references. If an object (a non primiative type) has a reference to it won't be deleted, but if it doesn't, i.e. there is no way to access it, it will be marked for deletion. BUT you cannot control when the deletion actually happens - might be this frame, might be the next.

Ultimately, 'null'ing' every variable/property (reference or otherwise), in my opinion is wasteful and very unmanegemable. Might be practical for smaller things, but when objects becoming larger with 100's of variables, you can't null all of them consistantly.

Down the track Flash Player will only optimise their GC for the right way of doing things, not the opposite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文