Phantomjs: page.close() does not free memory?

Created on 21 Jan 2016  路  5Comments  路  Source: ariya/phantomjs

When making many requests in series from the same script, phantomjs seems to increase in memory usage until it crashes.

I set up some handlers before opening the page to help see what is going on.

// ...

// set timeout
page.settings.resourceTimeout = 15000;

// check to see if the page is still open after it should have been closed
setTimeout(function() {
  try {
    page.close();
    console.log("the page WAS NOT closed properly in page.onResourceTimeout");
  } catch(e) {
    console.log("the page WAS closed properly in page.onResourceTimeout");
  }
}, page.settings.resourceTimeout + 5000);

// if the page is taking too long, close it and free memory
page.onResourceTimeout = function(request) {
  console.log("page.onResourceTimeout called");
  page.stop()
  page.close();
};

// close it and free memory if there is an error in the page
page.onError = function(msg, trace) {
  page.close();
};

Now open a ton of pages (these are done in series).

// start
page.open("http://example.com/", function(status) {

  // if error close page
  if(status !== "success") {
    page.close();

  // if success
  } else {

    // wait for results
    setTimeout(function() {
      try {

        // ...
        // do parsing
        // ...

        // close the page to free up resources
        page.close();

      } catch(err) {
        console.log("something went wrong while parsing");
        page.close();
      }

    // assume the page is loaded after 1 second
    }, 1000);
  }
});

};

The output from running the script contains a lot of these:

page.onResourceTimeout called
the page WAS NOT closed properly in page.onResourceTimeout

I renamed the .dmp file to .dmp.txt so Github would allow me to upload it.

PhantomJS has crashed. Please read the crash reporting guide at
<http://phantomjs.org/crash-reporting.html> and file a bug report at
<https://github.com/ariya/phantomjs/issues/new>.
Please attach the crash dump file:
  /tmp/1c14d90e-2758-2710-28ef5d6d-01275032.dmp

1c14d90e-2758-2710-28ef5d6d-01275032.dmp.txt

Is page.close() leaking memory or am I missing something?

Most helpful comment

I found a workaround.

// bad
setTimeout(function() {
  page.close();
}, 1000);

// works for whatever reason
setTimeout(function() {
  setTimeout(function() {
    page.close();
  }, 1);
}, 1000);

All 5 comments

I found a workaround.

// bad
setTimeout(function() {
  page.close();
}, 1000);

// works for whatever reason
setTimeout(function() {
  setTimeout(function() {
    page.close();
  }, 1);
}, 1000);

So do you still need page.stop()?

@mato75 I don't think so. The trick is the double setTimeout.

@mz3 Thank you. Hopefully there should be fix in phantomjs instead of relying on the work around

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sinojelly picture sinojelly  路  3Comments

olee picture olee  路  4Comments

maboiteaspam picture maboiteaspam  路  3Comments

Snowlav picture Snowlav  路  3Comments

julmot picture julmot  路  5Comments