26

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

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 !!

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.

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.

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 ?

Update 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.

lame_coder
  • 3,085
  • 3
  • 19
  • 21
  • What kinds of data structures do you have loaded on your web page to consume 833MB? That's crazy! I've never heard of a web application taking up that much memory. – Cameron Tinker Jun 30 '14 at 19:09
  • @CameronTinker When the application runs stand alone its not more 90MB and it stays there for its lifetime. Its a single page application built using EmberJS. The consumption 833MB is of the qunit test runner page .. – lame_coder Jun 30 '14 at 19:47

2 Answers2

10

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.

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

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.

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.

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
  • 1
    @lame_coder did you figure out how to access the `page` object from within your test suite to call this method? I'm running into this same problem with Phantom2 and Ember JS. – blimmer Apr 09 '15 at 22:10
  • In PhantomJS 2.0 through Selenium webdriver 1.2.0 it's `webdriver.executePhantomJS("phantom.page.clearMemoryCache()", [])`. However, this won't work on GCing the dom (like Iframes in a single page app), it just clears in memory cached images and such. – NielsK Jul 16 '15 at 07:39
  • @NielsK do you know if its available in 1.9.8? I tried `page.clearMemoryCache()` and `phantom.page.clearMemoryCache()` but in both cases I got a `ReferenceError: Can't find variable:` – Erin Drummond Aug 04 '15 at 20:34
  • 1
    @Erin_Drummond Nope, that feature was put in with the uograde to PhantomJS 2 – NielsK Aug 04 '15 at 21:52
1

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

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

editbin /LARGEADDRESSAWARE <pathto>/PhantomJS.exe
Anthony Wieser
  • 4,351
  • 1
  • 23
  • 25