简体   繁体   English

如何避免在Firefox中导致内存泄漏?

[英]How can I avoid causing memory leaks in Firefox?

It seems that there is a lot of information on memory leaks in IE and how web developers can avoid them, but I can't find much on avoiding leaks in FF. 似乎有很多关于IE中内存泄漏的信息以及Web开发人员如何避免它们,但我找不到有关避免FF泄漏的问题。 I've found lots of random tips on how end users can tweak their preferences, or tips for extension developers, but little on what I can do as a web developer to make sure my pages don't leak. 我发现了很多关于最终用户如何调整他们的偏好或扩展开发人员提示的随机提示,但很少关于我作为Web开发人员可以做些什么来确保我的页面不会泄漏。 Am I missing something? 我错过了什么吗? It seems lazy to just blame it on the user and say "you've got too many extensions". 把它归咎于用户并说“你有太多的扩展”似乎很懒。 Or are the major patterns the same as in IE -- circular references and all that? 或者主要模式与IE中的相同 - 循环引用和所有这些?

Also, if anyone knows of any tools to troubleshoot leaks in FF, that would be great. 此外,如果有人知道任何解决FF泄漏的工具,那将是很好的。 I found this: https://addons.mozilla.org/en-US/firefox/addon/2490/ But it's apparently just for chrome and extension development. 我发现了这个: https//addons.mozilla.org/en-US/firefox/addon/2490/但它显然只是用于chrome和扩展开发。

Outside of the design patterns to favour the only truely safe way is to test your pages thoroughly. 在设计模式之外,有利于唯一真正安全的方法是彻底测试您的页面。 To monitor the memory usage of the browser Task Manager is alright but Process Explorer provides more accurate results. 监视浏览器的内存使用情况任务管理器没问题,但Process Explorer提供了更准确的结果。

JavaScript is one cause of memory leaks but be careful with flash movies on pages too. JavaScript是内存泄漏的一个原因,但也要小心页面上的flash电影。 Our content team added a movie from our design department that used a thrid party transition affect and this swallowed 10Mb every 20s or so. 我们的内容团队在我们的设计部门添加了一部电影,该电影使用了第三方过渡效果,每20秒就吞下10Mb。 Just watching the movie loop through it was obvious in TaskManager to see the memory jump when the affect occured and the it never quite release it all back. 只是观看电影循环通过它在TaskManager中很明显看到当影响发生时内存跳转并且它永远不会完全释放它。

You can force to run a Garbage Collector in FireFox. 您可以强制在FireFox中运行垃圾收集器。 The Garbadge Collector will destroy & release objects that are not used anymore. Garbadge收集器将销毁和释放不再使用的对象。 The only possibility of "Leaking memory" with a Garbage Collector is not a "leak" but a reference which makes no sense: remove all references to objects that you don't want to use. 使用垃圾收集器“泄漏内存”的唯一可能性不是“泄漏”,而是一个没有意义的引用:删除对您不想使用的对象的所有引用。

Read more on this page: 在此页面上阅读更多内容:
http://adblockplus.org/blog/different-ways-to-force-garbage-collection http://adblockplus.org/blog/different-ways-to-force-garbage-collection

A bunch of what you read about how to avoid memory leaks in browsers is about how to avoid things that cause the browser to fail to reclaim memory that it should reclaim. 您阅读的有关如何避免浏览器中的内存泄漏的一堆内容是关于如何避免导致浏览器无法回收它应该回收的内存的事情。

However, a more substantial problem in many cases is about Web pages holding on to objects that they don't need anymore. 然而,在许多情况下,更实质的问题是关于持有他们不再需要的对象的网页。 It's only the browser's job to reclaim things that are no longer "reachable" -- that is, things that the script / page can't get to anymore. 回收浏览器的工作只是回收不再“可达”的东西 - 也就是说,脚本/页面无法再进入的东西了。 If you're accumulating objects in an array and not removing them when you're done with them, memory usage will go up as the array gets bigger, and there's nothing the browser can do about that. 如果你在数组中累积对象并且在完成它们时不删除它们,那么随着数组变大,内存使用率会上升,浏览器无法做到这一点。

To phrase that another way: that's an issue of a memory leak in the Web page rather than in the browser. 用另一种方式表达:这是网页中而不是浏览器中的内存泄漏问题。 And the tool you want for that is a memory profiling tool to examine the objects that are reachable in your page, so you can tell whether there's stuff in there that you should no longer be holding on to. 你想要的工具是一个内存分析工具来检查你的页面中可以访问的对象,这样你就可以判断那里有你不应该再坚持的东西了。 Writing such a tool for Firefox has been on my list of things to do for a while, but I haven't gotten around to it yet. 为Firefox编写这样的工具已经列入我要做的事情列表了一段时间,但我还没有解决它。 I think there might be some ongoing work on writing one that integrates into Firebug. 我认为可能正在进行一些正在编写的集成到Firebug中的工作。

I don't know if there is specific information for Firefox, but the generic tips still apply. 我不知道是否有针对Firefox的具体信息,但通用提示仍然适用。

I suggest that you closely examine all loops and recursive functions. 我建议你仔细检查所有循环和递归函数。 Re-use existing objects over creating new ones, and ensure that temporary objects and primitives exit scope so that they can be freed. 重复使用现有对象而不是创建新对象,并确保临时对象和基元退出范围,以便可以释放它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM