{"action":"created","issue":{"url":"https://api.github.com/repos/ariya/phantomjs/issues/11390","labels_url":"https://api.github.com/repos/ariya/phantomjs/issues/11390/labels{/name}","comments_url":"https://api.github.com/repos/ariya/phantomjs/issues/11390/comments","events_url":"https://api.github.com/repos/ariya/phantomjs/issues/11390/events","html_url":"https://github.com/ariya/phantomjs/issues/11390","id":15207094,"number":11390,"title":"memory leak with WebPage class, multiple page loads ( phantom 1.9.0 )","user":{"login":"jasons-novaleaf","id":814134,"avatar_url":"https://avatars.githubusercontent.com/u/814134?v=3","gravatar_id":"","url":"https://api.github.com/users/jasons-novaleaf","html_url":"https://github.com/jasons-novaleaf","followers_url":"https://api.github.com/users/jasons-novaleaf/followers","following_url":"https://api.github.com/users/jasons-novaleaf/following{/other_user}","gists_url":"https://api.github.com/users/jasons-novaleaf/gists{/gist_id}","starred_url":"https://api.github.com/users/jasons-novaleaf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasons-novaleaf/subscriptions","organizations_url":"https://api.github.com/users/jasons-novaleaf/orgs","repos_url":"https://api.github.com/users/jasons-novaleaf/repos","events_url":"https://api.github.com/users/jasons-novaleaf/events{/privacy}","received_events_url":"https://api.github.com/users/jasons-novaleaf/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":30,"created_at":"2013-06-06T05:57:08Z","updated_at":"2015-02-05T00:51:19Z","closed_at":null,"body":"There is a memory leak when reusing a phantomjs instance, averaging about 1.4mb per page  load in debian 7.  (for example, loading 200 pages results in 280mb memory use).  I have reprod this on Debian7 x64 and Windows8 x64\r\n\r\nI have included a phantomjs script that can be used to reproduce the memory leak.   I am following what seems to be \"best practice\" by invoking page.close() after the load is done, but that does not resolve the issue.    \r\n\r\nfyi, in addition to my repro script,  i have tried various combinations of tests  such as\r\n- close after page.onLoadFinished() has completed (not during)\r\n- reusing the same page object multiple times\r\n- not calling page.stop()\r\n\r\nnone of those had any positive impact on the memory leak problem.\r\n\r\nhere's my repro script, paste it into something like **memoryleak.js** and run from the command prompt.\r\n**NSFW**: The test domains are the top 100 domains by traffic, and that includes porn sites, so if you have some corporate IT network traffic monitoring going on, remove the naughty sites from the testUrls array.\r\n```javascript\r\n/// memoryleak.js\r\nfunction format(toFormat) {\r\n    var args = [];\r\n    for (var _i = 0; _i < (arguments.length - 1); _i++) {\r\n        args[_i] = arguments[_i + 1];\r\n    }\r\n    return (toFormat).replace(/\\{(\\d+)\\}/g, function (match, number) {\r\n        return typeof args[number] !== \"undefined\" ? args[number].toString() : match;\r\n    });\r\n}\r\n;\r\nvar system = require(\"system\");\r\nphantom.onError = function (msg, trace) {\r\n    try  {\r\n        console.log(\"phantom encountered an error.  exiting... \" + \"msg=\\\"\" + msg + \"\\\"\" + \" trace=\\\"\" + trace + \"\\\"/>\");\r\n    }finally {\r\n        phantom.exit(-1);\r\n    }\r\n};\r\n/** NSFW: The test domains are the top 100 domains by traffic, and that includes porn sites, so if you have some corporate IT network traffic monitoring going on, remove the naughty sites from the testUrls array. */\r\nvar testUrls = [\r\n    \"google.com\", \r\n    \"facebook.com\", \r\n    \"youtube.com\", \r\n    \"yahoo.com\", \r\n    \"amazon.com\", \r\n    \"bing.com\", \r\n    \"ebay.com\", \r\n    \"wikipedia.org\", \r\n    \"craigslist.org\", \r\n    \"linkedin.com\", \r\n    \"live.com\", \r\n    \"twitter.com\", \r\n    \"blogspot.com\", \r\n    \"aol.com\", \r\n    \"go.com\", \r\n    \"pinterest.com\", \r\n    \"msn.com\", \r\n    \"tumblr.com\", \r\n    \"cnn.com\", \r\n    \"ask.com\", \r\n    \"huffingtonpost.com\", \r\n    \"netflix.com\", \r\n    \"paypal.com\", \r\n    \"weather.com\", \r\n    \"conduit.com\", \r\n    \"espn.go.com\", \r\n    \"instagram.com\", \r\n    \"wordpress.com\", \r\n    \"bankofamerica.com\", \r\n    \"akamihd.net\", \r\n    \"imdb.com\", \r\n    \"chase.com\", \r\n    \"microsoft.com\", \r\n    \"about.com\", \r\n    \"avg.com\", \r\n    \"pornhub.com\", \r\n    \"comcast.net\", \r\n    \"foxnews.com\", \r\n    \"apple.com\", \r\n    \"walmart.com\", \r\n    \"xhamster.com\", \r\n    \"mywebsearch.com\", \r\n    \"wellsfargo.com\", \r\n    \"xvideos.com\", \r\n    \"yelp.com\", \r\n    \"imgur.com\", \r\n    \"nytimes.com\", \r\n    \"nbcnews.com\", \r\n    \"cnet.com\", \r\n    \"reddit.com\", \r\n    \"adobe.com\", \r\n    \"ehow.com\", \r\n    \"pandora.com\", \r\n    \"pch.com\", \r\n    \"hulu.com\", \r\n    \"zedo.com\", \r\n    \"etsy.com\", \r\n    \"flickr.com\", \r\n    \"outbrain.com\", \r\n    \"optmd.com\", \r\n    \"indeed.com\", \r\n    \"livejasmin.com\", \r\n    \"zillow.com\", \r\n    \"target.com\", \r\n    \"xnxx.com\", \r\n    \"homedepot.com\", \r\n    \"redtube.com\", \r\n    \"answers.com\", \r\n    \"thepiratebay.sx\", \r\n    \"att.com\", \r\n    \"shopathome.com\", \r\n    \"wikia.com\", \r\n    \"dailymail.co.uk\", \r\n    \"usps.com\", \r\n    \"babylon.com\", \r\n    \"ups.com\", \r\n    \"bestbuy.com\", \r\n    \"youporn.com\", \r\n    \"reference.com\", \r\n    \"godaddy.com\", \r\n    \"groupon.com\", \r\n    \"deviantart.com\", \r\n    \"usatoday.com\", \r\n    \"pof.com\", \r\n    \"capitalone.com\", \r\n    \"bbc.co.uk\", \r\n    \"washingtonpost.com\", \r\n    \"match.com\", \r\n    \"drudgereport.com\", \r\n    \"mlb.com\", \r\n    \"tripadvisor.com\", \r\n    \"pogo.com\", \r\n    \"verizonwireless.com\", \r\n    \"blogger.com\", \r\n    \"buzzfeed.com\", \r\n    \"doublepimp.com\", \r\n    \"inksr.com\", \r\n    \"delta-search.com\", \r\n    \"fedex.com\", \r\n    \"inksdata.com\", \r\n    \"oyodomo.com\", \r\n    \"aweber.com\", \r\n    \"abcnews.go.com\", \r\n    \"vimeo.com\", \r\n    \"hootsuite.com\", \r\n    \"bleacherreport.com\", \r\n    \"lowes.com\", \r\n    \"yellowpages.com\", \r\n    \"americanexpress.com\", \r\n    \"tube8.com\", \r\n    \"yieldmanager.com\", \r\n    \"salesforce.com\"\r\n];\r\nvar readyToDispose = false;\r\nvar _testsRemaining = 1000;\r\nvar openRequest;\r\nvar lastSeen;\r\nvar lastSeenStartTime;\r\nfunction disposePage() {\r\n    openRequest.close();\r\n    openRequest = null;\r\n    readyToDispose = false;\r\n}\r\nfunction _tryNextText() {\r\n    if(openRequest != null) {\r\n        //if (readyToDispose) {\r\n        //\tdisposePage();\r\n        //\treturn false;\r\n        //}\r\n        if(lastSeen == openRequest) {\r\n            var elapsed = Date.now() - lastSeenStartTime;\r\n            if(elapsed > 10000) {\r\n                console.log(format(\"PAGE LOAD TIMEOUT! aborting url={0},  pageLen={1}\", openRequest.url, openRequest.content.length));\r\n                //timed out, next loop will reacquire\r\n                openRequest.stop();\r\n                //openRequest.close();\r\n                //openRequest = null;\r\n                //lastSeen = null;\r\n                //lastSeenStartTime = null;\r\n                            } else {\r\n                //not yet timed out\r\n                return false;\r\n            }\r\n        } else {\r\n            //set our last seen then wait for next loop\r\n            lastSeen = openRequest;\r\n            lastSeenStartTime = Date.now();\r\n        }\r\n        return false;\r\n    }\r\n    var index = _testsRemaining % testUrls.length;\r\n    var targetUrl = \"http://www.\" + testUrls[index];\r\n    _testsRemaining--;\r\n    if(_testsRemaining <= 0) {\r\n        console.log(\"TESTS COMPLETE!  check your memory usage\");\r\n        clearInterval(loopHandle);\r\n        return false;\r\n    }\r\n    console.log(format(\"[{0}] = {1} starting...\", _testsRemaining, testUrls[index]));\r\n    var thisPage = require(\"webpage\").create();\r\n    openRequest = thisPage;\r\n    openRequest.onLoadFinished = function (status) {\r\n        if(openRequest == thisPage) {\r\n            console.log(format(\"got page, url ={0}, targetUrl={1}  pageLen={2}\", openRequest.url, targetUrl, openRequest.content.length));\r\n            //readyToDispose = true;\r\n            openRequest.stop();\r\n            disposePage();\r\n        } else if(openRequest == null) {\r\n            console.log(\"ERROR? page opened while openRequest==null,  pageurl=\" + targetUrl);\r\n            //phantom.exit(-1);\r\n                    } else {\r\n            console.log(format(\"ERROR!  WRONG PAGE OPEN! got page, openPageurl ={0}, targetUrl={1}  pageLen={2}\", openRequest.url, targetUrl, openRequest.content.length));\r\n            phantom.exit(-1);\r\n        }\r\n    };\r\n    openRequest.open(targetUrl);\r\n    return true;\r\n}\r\ntestUrls.length = _testsRemaining > testUrls.length ? testUrls.length : _testsRemaining;\r\nconsole.log(\"running tests, count = \" + _testsRemaining);\r\nconsole.log(\"setting interval \");\r\nvar loopHandle = setInterval(function () {\r\n    _tryNextText();\r\n}, 500);\r\n\r\n\r\n```\r\n\r\nPS: run that script for long enough (usually around 400 page loads) and the phantomjs exe crashes, sometimes silently, sometimes with segfaults\r\n"},"comment":{"url":"https://api.github.com/repos/ariya/phantomjs/issues/comments/72973587","html_url":"https://github.com/ariya/phantomjs/issues/11390#issuecomment-72973587","issue_url":"https://api.github.com/repos/ariya/phantomjs/issues/11390","id":72973587,"user":{"login":"baer","id":940667,"avatar_url":"https://avatars.githubusercontent.com/u/940667?v=3","gravatar_id":"","url":"https://api.github.com/users/baer","html_url":"https://github.com/baer","followers_url":"https://api.github.com/users/baer/followers","following_url":"https://api.github.com/users/baer/following{/other_user}","gists_url":"https://api.github.com/users/baer/gists{/gist_id}","starred_url":"https://api.github.com/users/baer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/baer/subscriptions","organizations_url":"https://api.github.com/users/baer/orgs","repos_url":"https://api.github.com/users/baer/repos","events_url":"https://api.github.com/users/baer/events{/privacy}","received_events_url":"https://api.github.com/users/baer/received_events","type":"User","site_admin":false},"created_at":"2015-02-05T00:51:19Z","updated_at":"2015-02-05T00:51:19Z","body":"This looks like a duplicate issue:\r\nhttps://github.com/ariya/phantomjs/issues/12903"}}