wswebcreation / webdriver-image-comparison

MIT License
43 stars 36 forks source link

How to prevent memory leaks when initializing mutliple browsers #72

Closed websaid closed 3 years ago

websaid commented 4 years ago

Describe your question with as much detail as possible When having a loop which creates browsers and takes screenshots, after the session/iteration was closed, a chunk of memory is still being used and not garbage collected.

After the first iteration: image After 6 iterations image

Is there a way to clean up this memory? Thanks and have a great day!

If it's about a specific piece of code, try and include some of it to support your question. Code used to create the Heapsnapshots:

const { remote } = require("webdriverio");
const WdioImageComparisonService = require('wdio-image-comparison-service').default;
let folderActual
let browsers = []
const heap = require("heapdump")
let testBrowser
let testResults
let env
let window
callTests(new Array(10)).then(erg => {
    res(erg)
})
async function main(nsc, i) {
    //console.log(`Started test: ${testcase}@${nsc} (${i+1}/${allNSC.length})`)
    const browser = await remote({
        capabilities : {
            browserName: "chrome",
        },
        logLevel: "error",
        outputDir: __dirname + "../../../logs/"
    }).catch(err => console.log(err));
    browsers[i] = browser
    let wdioImageComparisonService = new WdioImageComparisonService({
        "savePerInstance": true,
        "clearRuntimeFolder": true,
        "autoSaveBaseline": true,
        "blockOutStatusBar": true,
        "blockOutToolBar": true,
        "disableCSSAnimation": false,
        "fullPageScrollTimeout": 2500,
        "clearFolder": true,
        formatImageName: `{tag}`,
        screenshotPath: `.tmp/`,
    });

    global.browser = browser;
    browser.defaultOptions = wdioImageComparisonService.defaultOptions;
    browser.folders = wdioImageComparisonService.folders
    wdioImageComparisonService.before(browser.capabilities)

    let url = "https://www.nytimes.com/"
    await browser.url(url)
    await heap.writeSnapshot(__dirname + "/../heaps/"+i+"before.heapsnapshot")
    let check = await browser.checkFullPageScreen(i, {
        "fullPageScrollTimeout": 2500
    })
    await heap.writeSnapshot(__dirname + "/../heaps/"+i+"after.heapsnapshot")

    await browser.deleteSession()
    await delete browser
    await delete browsers[i]
}

function callTests(arr, i) {
    return new Promise(res => {
        if (i == undefined) i = 0
        let error = false
        main(arr[i], i).catch(async e => {
            console.error(e, arr[i])
            error = e
            await browsers[i].deleteSession();
            await delete browsers[i]
        }).then(async erg => {
            if (i < arr.length - 1) return res(callTests(arr, ++i))
            else res()
        });
    })
}

function setupTest(testdata) {
    env = testdata.env
    allNSC = testdata.nscs
    testBrowser = testdata.browser
    testrunId = testdata.testrunId
    port = testdata.port
    testrunName = testdata.name
    window = testdata.window
    config.remote.capabilities = {
        browserName: testBrowser,
        //'goog:chromeOptions': {
        //    args: ['--window-size=' + testdata.window.width + ',' + testdata.window.height],
        //}
    }
    testResults = { testcase: testcase, teststeps: 1, teststepNames: ["mainPage"], data: [], browser: testBrowser }
    return new Promise((res, rej) => {

    })
}
module.exports.setupTest = setupTest

Environment (please complete the following information):

wswebcreation commented 4 years ago

Hi @Westsaid

This lib just uses the the provided libs by WebdriverIO to create screenshots. As far as I can see this is not a problem within this module but within, or the browser or WebdriverIO. Have you also checked the WDIO project? There's not a lot I can do here

websaid commented 4 years ago

Hey @wswebcreation,

thanks for your answer! I will take this over to the WDIO Github and report this as an issue there. Thanks for your help and the work!

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.