javascript Phantomjs 因高内存消耗而死

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24436460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:47:30  来源:igfitidea点击:

Phantomjs dies by high memory consumption

javascriptmemory-leakstfsphantomjs

提问by lame_coder

We are using phantomjs to run our qunit tests page on our TFS build server. Our version of test runner is built from below example

我们正在使用 phantomjs 在我们的 TFS 构建服务器上运行我们的 qunit 测试页面。我们的测试运行器版本是从以下示例构建的

https://github.com/ariya/phantomjs/blob/master/examples/run-qunit.js

https://github.com/ariya/phantomjs/blob/master/examples/run-qunit.js

Over a period of time number of tests increased from hundreds to couple of thousands and on a fine day phantomjs started crashing. It literally dies saying upload the dump and when you see the dump it 0kb !!

在一段时间内,测试数量从数百个增加到数千个,并且在晴朗的一天 phantomjs 开始崩溃。它真的死了说上传转储,当你看到转储它 0kb !

When we took a closer look at it on process explorer we found that memory consumption by phantomjs keeps going up as phantomjs is running tests and eventually crashes somewhere 833MB.

当我们在进程浏览器上仔细查看它时,我们发现 phantomjs 的内存消耗不断增加,因为 phantomjs 正在运行测试并最终在某个地方崩溃 833MB。

Yes the same amount of memory was being utilized by chrome and IE ! And Yes-Yes our tests were leaking memory :(. We did fixed it, memory utilization is lowered by 50% on chrome and IE and we expected phantomjs will handle it now. But no, phantomjs still kept crashing, process explorer shows same memory consumption.

是的,chrome 和 IE 使用了相同数量的内存!是的 - 是的,我们的测试正在泄漏内存:(。我们确实修复了它,Chrome 和 IE 上的内存利用率降低了 50%,我们希望 phantomjs 现在可以处理它。但是不,phantomjs 仍然不断崩溃,进程资源管理器显示相同的内存消耗。

http://phantomjs.org/api/webpage/method/close.html

http://phantomjs.org/api/webpage/method/close.html

According to above documentation phantomjs releases heap allocation just on close ? Could that be the reason why our fixed test consumed less memory on chrome but not phantomjs ? And last how to fix this ? How to make phantomjs keep garbage collecting javascript objects to reduce heap allocation ?

根据上面的文档,phantomjs 在关闭时释放堆分配?这可能是我们的固定测试在 chrome 上消耗较少内存而不是 phantomjs 的原因吗?最后如何解决这个问题?如何让 phantomjs 保持垃圾收集 javascript 对象以减少堆分配?

Update 1 - 07/28

更新 1 - 07/28

We took a work around. I did modified my script to execute my tests module by module. In loop after executing all tests for a module I call page.close so it releases the memory for each module and never keeps building the dead heap of objects. Not closing this question since since its a workaround and not a solution. Hope creators will fix this sometime.

我们做了一个工作。我确实修改了我的脚本以逐个模块执行我的测试。在对一个模块执行所有测试后的循环中,我调用 page.close 以便它释放每个模块的内存并且从不继续构建对象的死堆。不关闭这个问题,因为它是一种解决方法而不是解决方案。希望创作者会在某个时候解决这个问题。

回答by Cameron Tinker

There is a static method, QWebPageSettings::clearMemoryCache, that invokes WebKit's garbage collection. However, it clears all QWebPage memory cache for every instantiated QWebPage object and is therefore, currently, unsuitable for including as an option in PhantomJS.

有一个静态方法 ,QWebPageSettings::clearMemoryCache可以调用 WebKit 的垃圾收集。但是,它会为每个实例化的 QWebPage 对象清除所有 QWebPage 内存缓存,因此目前不适合作为 PhantomJS 中的选项包含在内。

The Github pull request is available here:
https://github.com/ariya/phantomjs/pull/11511
Here's the Google Groups discussion:
https://groups.google.com/forum/#!msg/phantomjs/wIDp9J7B-bE/v5U31_mTbswJ

Github 拉取请求可在此处获得:
https: //github.com/ariya/phantomjs/pull/11511
这是 Google Groups 讨论:https://groups.google.com/forum/#!msg/phantomjs/wIDp9J7B-bE
/v5U31_mTbswJ

Until a workaround is available, you might break up your unit tests into blocks on separate pages. It will take a change to QtWebkit's implementation and how memory/cache is handled across QWebPage objects.

在解决方法可用之前,您可以将单元测试分解为不同页面上的块。它将改变 QtWebkit 的实现以及如何跨 QWebPage 对象处理内存/缓存。

Update September 2014: https://github.com/ariya/phantomjs/commit/5768b705a0
It looks like support for clearing memory cache was added, but there is a note about my original comment in the commit.

2014 年 9 月更新:https: //github.com/ariya/phantomjs/commit/5768b705a0
似乎添加了对清除内存缓存的支持,但在提交中有关于我的原始评论的注释。

回答by Anthony Wieser

I managed to work around it by setting the /LARGEADDRESSAWARE flag

我设法通过设置 /LARGEADDRESSAWARE 标志来解决它

If you have visual studio installed, run from a visual studio command prompt

如果您安装了 Visual Studio,请从 Visual Studio 命令提示符运行

editbin /LARGEADDRESSAWARE <pathto>/PhantomJS.exe