Flex 4:垃圾收集不起作用
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试删除事件侦听器,我认为将其挂在那里会阻止 GC 收集您的对象。
Try removing the event listener, I think having it hanging on there will prevent the GC from collecting your object.
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.