[REGRESSION]: Memory leak related to route interception
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)
Heap snapshot for v1.4.0
Heap snapshot for v1.6.2
This is indeed a known issue, thank you for filing.
+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?
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.
Similar to https://github.com/microsoft/playwright/issues/4549, closing.


