Flex 4:垃圾收集不起作用

发布于 2024-10-03 08:47:26 字数 564 浏览 3 评论 0原文

我有一个 TitleWindow 组件,通过 PopUpManager.addPopUp() 显示。当我关闭组件时,我调用 closePopUp 事件,该事件将对象的变量设置为 null,如下所示:

// application
private var myObject:MyObject;

private function openPopUp():void
{
    myObject = new MyObject();
    myObject.addEventListener('closePopUp', closePopUp);
    PopUpManager.addPopUp(myObject, this, true);
}

private function closePopUp(e:Event):void
{
    myObject = null;
}

但是在调试模式下,我可以看到 myObject 被设置为 null,但内存使用量并没有减少。当我再次打开该组件时,内存使用量仍与之前相同。

我本以为当 myObject 变量设置为 null 时,GC 会回收该对象并释放内存。知道为什么这没有发生吗?

I have a TitleWindow component that I'm displaying via PopUpManager.addPopUp(). When I close the component, I'm calling the closePopUp Event which sets the object's variable to null, as follows:

// application
private var myObject:MyObject;

private function openPopUp():void
{
    myObject = new MyObject();
    myObject.addEventListener('closePopUp', closePopUp);
    PopUpManager.addPopUp(myObject, this, true);
}

private function closePopUp(e:Event):void
{
    myObject = null;
}

However in debug mode I can see myObject being set to null, but the memory usage doesn't decrease. When I open the component again, the memory usage remains the same as before.

I would have thought when the myObject variable is set to null, GC recycles the object and frees up memory. Any idea why this is not happening?

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

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

发布评论

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

评论(2

-柠檬树下少年和吉他 2024-10-10 08:47:26

尝试删除事件侦听器,我认为将其挂在那里会阻止 GC 收集您的对象。

Try removing the event listener, I think having it hanging on there will prevent the GC from collecting your object.

烈酒灼喉 2024-10-10 08:47:26

Flash Player 中的垃圾收集并不是一门精确的科学,其工作原理与大多数人的预期略有不同...总而言之,它仅在需要更多内存时释放内存。这些文章更详细地解释了它:

http://www.adobe.com/devnet /flashplayer/articles/garbage_collection.html
http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html
http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html

此外,正如 Dein 所说,事件侦听器是 Flash 中内存泄漏的最常见原因。您必须非常小心并始终尝试将其删除。

The garbage collection in Flash Player is not an exact science and works a little different to what most people expects... To summarize it, it only frees up memory when it needs more. These articles explain it with more detail:

http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html
http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html

Also, as Dein says, the event listeners are the most common cause for memory leaks in Flash. You have to be very careful and always try to remove them.

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