-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Memory increases when same context is used #6319
Comments
Currently the objects for e.g. request/response/route get only flushed when a new context is created. For testing you typically create a new context for each test. I folded a few issues into this one of users who also faced into that, so we can better keep track of it and might find a workaround for it in the future. |
I am recreating the page every 1 hour this is the only work around I have found to keep the memory stable. |
|
Hey @mxschmitt Are there some other similar quirks that can be used to lower memory/CPU usage? I am starting to look into performance and parallelization of our tests and things like this would be helpful. Thanks! |
See here: https://playwright.dev/docs/intro |
I think being able to have the page flush responses when new ones are received would be a useful feature, and along the lines of the way a typical browser handles responses. Maybe have it as a flag we can set? While headless browsers have their origins in browser testing, and browser testing continues to be a major use case, data science is a rapidly growing field, and scraping is a major selling point for using headless browsers. When scraping, you may not want to create an entirely new context with each data retrieval, as the cookies may be important for storing complex states or tokens. This leads to difficulties with the current Playwright implementation. Scrapers are primarily needed when lightweight APIs aren't available or practical, and unfortunately in the modern web heavy pages of multiple megabytes of data can be included in each response. If you're getting HTTP responses every two seconds, you can easily amass over 1GB of additional memory leakage within an hour if all of those responses are being stored. In my case, after 43 minutes my playwright |
I have a case where I make several requests per minute, it leaks memory all over the place. I've tried closing pages, closing contexts, closing browsers, catching all the onClose events and reclosing everything all together, even closing the main playwright instance all the time, nothing works, it still leaks memory and at some point the GC thrashes completely with the CPU setting on fire. Please provide and/or document a way to fully release all resources so that we can clear it out every now and then. Please don't just focus on testing, it's full of other use cases where playwright is needed to run long long times. |
Is there a way to properly dispose the whole thing without terminating the process launching it? |
I have the exact same issue. Use case: scrapping/botting. Here is a ultra-simple repro: import { chromium } from 'playwright'
const setup = async () => {
const browser = await chromium.launch({ headless: false })
let page = await browser.newPage()
let j = 0
while (true) {
for (let i = 0; i < 20; i++, j++) {
console.log(j, i)
await page.goto('https://v3.vuejs.org/guide/introduction.html#declarative-rendering')
}
console.log('Trying to create a new context, does not fix the leak!')
await page.close()
page = await browser.newPage()
}
}
setup() Both this and #8775 are probably duplicates. It shows that there is an issue with playwright itself and not with chromium or webkit. For readers having the same issue, as a temporary workaround you might use something like PM2 with the memory limit config. It will restart the process when the limit is reached. |
Hey guys - any ETA on when is this going to be fixed? |
Something that can help if it hasn't already been done is optimizing requests, ignoring unnecessary things like images. |
@LuizFelipeNeves images can be very important, depending on what you are doing. Automated browsers have lots of different use cases. The project that I used playwright for needed to take screenshots of webpages, images and all. The problem that Playwright is having isn't a request issue, but a memory leak issue. Reducing the size of the requests only makes it so that it takes longer for the memory overflow to occur, it doesn't fix the underlying problem. I think we can all agree that we should be able to instruct playwright to not make an infinite cache of request response data between page loads, while caching the things that are designed to persist between page loads like cookies and local storage. Basically, a setting to behave more like a browser. So much of the issue we're facing with playwright was solved by the early browser developers dating back to Netscape's cookies in 1994; there's no need to reinvent the wheel here. |
Using the code I use in my project? |
I think it's a stupid design in playwright. Any programmer will hate the memory problem and that is not in his control. |
@vonkoff It's not Windows compliant. On Windows machine, to kill a Node.js server, and you don't have any other Node processes running, you can tell your machine to kill all processes named
And if the processes still persist, you can force the processes to terminate by adding the
|
Besides not being a proper solution, I would just like to make it clear that restarting everything is just not feasible is some cases, like when you are working on a infinite scroll page. If you restart, you are doomed. Thought I would reiterate on this just so that no one thinks there is a good way to solve this issue. The bug should be fixed. |
@tzbo tough comment, why dont you make something better? |
This is the usual dump f. take people come with when they have zero priority to solve a ticket. |
It's tough and it's a long time ticket. There is no better solution in two years to solve memory problem. Do you think it's a good design? Maybe the original intention is to simplify usage. Of courese it's more simple than puppeteer in some senario. But I think at least it should keep some release memory funtions which mean I call these funtions. I promise I will not use previouse response and .... |
👋 @gigitalz, dgtlmoon isn't well socialised Ignoring is the only language he speaks Regards 🥩 |
This is also a very confusing issue for us. As long as we keep opening new pages within the loop, the memory keeps increasing continuously. We have encountered several OOM errors recently. |
We have all the feedback we need for this issue and it is currently pending due to prioritization. I'll disable the comments since they no longer add actionable details to the issue. |
Unbounded heap growth should be mitigated by ffd20f4. The heap will still saturate to a certain size (1K handles per object type, ~50-100Mb on average), but will stop growing afterwards. |
…t#24169) Partial fix for microsoft#6319 After this fix, the following scenario won't leak and the context state (cookies, storage, etc) can be reused by the new page sessions: ```js for (let i = 0; i < 1000; ++i) { const page = await context.newPage(); await page.goto('...'); await page.close('...'); } ```
Apart from making demands to people I dont know like some previous commenters, I would like to thank Pavel for adding that heap stack test to npm, that's a super cool idea. And generally thank the maintainers for their incredible work here, it's a highly complex project! |
…t#24169) Partial fix for microsoft#6319 After this fix, the following scenario won't leak and the context state (cookies, storage, etc) can be reused by the new page sessions: ```js for (let i = 0; i < 1000; ++i) { const page = await context.newPage(); await page.goto('...'); await page.close('...'); } ```
I face this issue in Azure Dev Ops agent pipeline. Locally all test run fine.. we have 200+ test and 5-6 run in parallel at a time.. after 8 minutes of running build it fails with <--- Last few GCs ---> |
After update from 1.29.1 to the latest 1.40.0, started to receive the error: So, for now decided to downgrade to prev version. PS. I have about 800 scenarios, each of them has about 15 separate steps. In average, on the 300-350th test they become to fail. |
We need unfortunately a reproduction case in order to debug issues like that. A small repository would be ideal. I'm going to lock this for now, so others can re-file and we can work on the missing scenarios, thanks! |
Context:
Describe the bug
I'm watching full-js apps (e.g react/angular websites). I initialize one instance, 1 browser and 1 page.
I keep the page in cache & retrieving content every 2 seconds.
After 1/2hours, the memory goes crazy. I tried to
reload()
the page every 30 minutes. It doesn't free the memory.Only way to free the memory is closing the page and recreating a new one.
What could be the source of this memory leak? I suppose
reload()
frees the javascript-vm so it must be a leak internally to the pageThe text was updated successfully, but these errors were encountered: