playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[REGRESSION]: Memory leak related to route interception

Open josemmo opened this issue 6 years ago • 4 comments

Context:

  • GOOD Playwright Version: 1.3.0
  • BAD Playwright Version: 1.4.0
  • Operating System: Windows 10
  • Extra: Node.js 14.14.0

Code Snippet

process.env.DEBUG = 'pw:api';
const { chromium } = require('playwright');

async function startWorker(context) {
    const page = await context.newPage();
    await page.goto('https://angular.io/');

    // Click on a bunch of random links
    for (let i=0; i<200; i++) {
        await page.evaluate(() => {
            const links = [...document.querySelectorAll('body [href]')].filter(elem => {
                return !/^https?:/.test(elem.getAttribute('href'));
            });
            const link = links[~~(Math.random()*links.length)];
            link.click();
        })
        await page.waitForTimeout(1000);
    }
}

(async() => {
    const browser = await chromium.launch({headless: true});
    const context = await browser.newContext({viewport: null});

    // Create multiple workers to speed-up things
    const promises = [];
    for (let i=0; i<10; i++) {
        promises.push(startWorker(context));
    }
    await Promise.all(promises);

    console.log('Finished!');
    // Not closing browser on purpose to inspect heap
})();

Describe the bug From Playwright v1.4.0, Request and Response instances persist on the heap. This seems to be specially impactful in websites that use AJAX navigation.

Heap snapshot for v1.3.0 (unaffected) v1.3.0

Heap snapshot for v1.4.0 v1.4.0

Heap snapshot for v1.6.2 v1.6.2

josemmo avatar Nov 22 '20 14:11 josemmo

This is indeed a known issue, thank you for filing.

dgozman avatar Nov 23 '20 18:11 dgozman

+1 was just looking to open an issue, facing a similar problem. digging into the heap snapshots it looks like all the commands transferred through websocket persisted in heap. for some of my long-running scenarios it leads to really huge leak over 2GB.

If it is intended behaviour(persisting command history) - is there a way to flush it manually?

wowhops avatar Nov 23 '20 20:11 wowhops

We assume that you use new browser context for each new test, so we accumulate resources and other objects in the scope of the browser context. Closing the context and creating the new one will flush it.

pavelfeldman avatar Nov 26 '20 06:11 pavelfeldman

Similar to https://github.com/microsoft/playwright/issues/4549, closing.

pavelfeldman avatar Jan 18 '21 17:01 pavelfeldman