调试 FLEX/AS3 内存泄漏
我有一个相当大的 Flex & Papervision3D 应用程序可连续创建和销毁对象。 它还加载和卸载 SWF 资源文件。 当它运行时,SWF 会慢慢消耗内存,直到大约 2GB,这时播放器就会崩溃。 显然,我非常确定我放弃了对我不再需要的实例的引用,并期望 GC 能够完成其工作。 但我很难弄清楚问题出在哪里。
我尝试过使用探查器及其捕获内存快照的选项等 - 但我的问题仍然难以回避。 我认为使用调试 Flash 播放器也存在已知问题? 但我对发布版本的使用也没有感到高兴。
如何使用 FLEX/AS3 追踪内存泄漏问题? 您使用过哪些定位消费的策略、技巧或工具?
I have a pretty big Flex & Papervision3D application that creates and destroys objects continually. It also loads and unloads SWF resource files too. While it's running the SWF slowly consumes memory til about 2GB when it croaks the player. Obviously I am pretty sure I let go of reference to instances I no longer want with expectation the GC will do its job. But I am having a heck of a time figuring out where the problem lies.
I've tried using the profiler and its options for capturing memory snapshots, etc - but my problem remains evasive. I think there are known problems using debug Flash player also? But I get no joy using the release version either.
How do you go about tracking down memory leak problems using FLEX/AS3 ? What are some strategies, tricks, or tools you have used to locate consumption
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通常在我创建的每个类中实现一个清理方法(因为 AS 没有析构函数)。 我注意到 GC 的主要问题是事件监听器。 除了 dirkgently 所说的之外,还要尝试避免匿名侦听器函数(因为您无法显式删除它们)。 以下是一些您可能会觉得有用的链接:
I usually implement a cleanup method in every class I make (since AS doesn't have destructors). The main problem I've noticed with the GC is with event listeners. Additional to what dirkgently said, also try to avoid anonymous listener functions (as you can't explicitly remove them). Here are a few links you may find useful:
我偶然发现了一些解释如何在 Flex Builder 中使用 Flex Profiler 的内容,它对我调试内存泄漏有很大帮助。 我绝对建议尝试一下。 它非常容易使用。 我在分析应用程序时发现的一些事情:
避免使用集合(至少是大型集合)作为值对象的属性。 我的 Cairngorm 应用程序中有多种类型的值对象类,每个类都有一个“children”属性,它是一个 ArrayCollection,用于过滤。 在进行分析时,我发现这些是我最大的内存消耗者之一,因此我更改了应用程序,将“parentId”存储为
int
并使用它进行过滤。 使用的内存被大幅削减。 像这样的东西:旧方式:
新方式:
我还建议在不再需要事件侦听器时将其删除。
I stumbled across something explaining how to use Flex Profiler in Flex Builder and it was a HUGE help to me in debugging memory leaks. I would definitely suggest trying it out. It's very easy to use. Some things I found when profiling my applications:
Avoid using collections (at least LARGE collections) as properties of Value Objects. I had several types of Value Object Classes in my Cairngorm application, and each had a "children" property which was an
ArrayCollection
, and was used for filtering. When profiling, I found that these were one of my biggest memory eaters, so I changed my application to instead store the "parentId" as anint
and use this for filtering. The memory used was cut drastically. Something like this:Old way:
New Way:
I would also suggest removing event listeners when they are no longer needed.
由于此类问题,我开发了一个开源库,可以帮助监视您在任何给定时间运行的所有事件。 它非常容易实现,我在 10-15 分钟内重构了项目,将它们转换为使用我开发的 EventController。
基本上,对于您的场景,我将遍历所有事件并将它们替换为:
obj.addEventListener(...);
到 :
EC.add(obj,...);
其余的与注册事件相同,并使用 EC.log() 在您想要的任何时候轻松查看所有事件;
所有详细信息和文档都在我的网站上,我很想知道这是否对您有帮助以及您是否开始使用它。 如果您有任何好的或坏的反馈,请随时发布,我会调查!
该网站是:
http://fla.as/ec/
because of issues like this I've developed a open source library that helps monitor all the events your running at any given time. its really easy to implement and i've re-factored projects in 10-15 minutes converting them to use the EventController i've developed.
basically for your scenario i would run through all the events and replace them from:
obj.addEventListener(...);
to :
EC.add(obj,...);
the rest is the same what that would do is register the event and make it crazy easy to see all your events at any point you want using the EC.log();
all the details and documentation are on my site i would love to know if this helps you and if you start working with it. if you have any feedback good or bad please feel free to post it up and i would look into it!
the site is:
http://fla.as/ec/
如果内存泄漏呈指数级增长,则可能意味着 GC 无法完成其工作。 查看您的代码,看看在哪里可以减少对象的引用计数(通过将它们设置为
null
)。 使事件处理程序变弱。 并重新配置。If your memory leak grows exponentially, it probably means GC is failing to do its job. Take a look at your code and see wherever you can decrease your objects' reference counts (by setting them to
null
). Make event-handlers weak. And re-profile.