-
Notifications
You must be signed in to change notification settings - Fork 245
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]: Playwright running in .NET hosted services seems to leak memory #2962
Comments
Thank your for your bug report. When running it via VS on Windows, it gave me these results: The increased heap size, seems to be caused by the Also when doing the following, it seems to not leak for me: using Microsoft.Playwright;
Console.WriteLine($"Started process under PID {System.Diagnostics.Process.GetCurrentProcess().Id}");
while (true) {
await CreateScreenshotAsync(1920, 1080);
GC.Collect();
Console.WriteLine($"TotalMemory: {GC.GetTotalMemory(false)}");
}
async Task CreateScreenshotAsync(uint width, uint height)
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.SetViewportSizeAsync((int)width, (int)height);
await page.GotoAsync("https://playwright.dev/dotnet/");
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "Screenshot.png", Type = ScreenshotType.Png });
Console.WriteLine("Screenshot created");
} Ideally we are able to create a repro without AspNetCore out of it. Do you observe the same without AspNetCore? |
Thank you for getting back to me. Even when adding However, when running a console app with the following code, I don't see this behavior: using Microsoft.Playwright;
Console.WriteLine($"Started process under PID {System.Diagnostics.Process.GetCurrentProcess().Id}");
PeriodicTimer timer = new(TimeSpan.FromSeconds(15));
while (await timer.WaitForNextTickAsync(CancellationToken.None))
{
await CreateScreenshotAsync(1920, 1080);
GC.Collect();
Console.WriteLine($"TotalMemory: {GC.GetTotalMemory(false)}");
}
async Task CreateScreenshotAsync(uint width, uint height)
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.SetViewportSizeAsync((int)width, (int)height);
await page.GotoAsync("https://playwright.dev/dotnet/");
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "Screenshot.png", Type = ScreenshotType.Png });
Console.WriteLine("Screenshot created");
} In case you're asking why I care: due to the problematic behavior in my ASP.NET Core app (see here) which runs on my Raspberry Pi 4 in a
So I see the following follow-up questions:
|
We are facing the similar problem with this, we are using the asp.net core and we are keeping doing the screenshot. the memory is pretty slow, our service will continue leak and eat up all the 8G memory for 2 days. |
@shuowpro: which version of Playwright are you using? I'm now on Before, it was constantly crashing after some days (each color represents a new container): |
I'll close it for now, since this issue is unfortunately not actionable for us. It looks like some bug in ASP.NET whereI recommend filing against them. Thanks for your understanding and happy that it seems resolved! |
@mxschmitt ...and the ASP.NET Core guys will argue the same: it looks like some bug in Playwright 😥 |
@mu88 do you have a reference to their response? I hope to dedicate some time to it later this week or reach out to some more experienced .NET experts in that area. |
So happy to find this issue... and good sleuthing @mu88! We have a container that grows to many gigabytes in size (over a few days), OOMs, is killed, is restarted, ... etc., etc., etc. We failed to find the cause and were completely stumped. And Months later, I found this issue, and it suddenly makes sense! (Environment: playwright invoked periodically in hosted service, .net7 linux container) |
Version
1.45.0
Steps to reproduce
dotnet build
pwsh Web\bin\Debug\net8.0\playwright.ps1 install
dotnet run
)info: NewScreenshotCreator[0] Screenshot created
)Expected behavior
The app should not use more memory over time (both managed and unmanaged).
Actual behavior
The app uses more memory over time (both managed and unmanaged).
Additional context
I quickly analyzed one memory dump. Without further understanding of what's going on in Playwright internally, I don't want to speculate about the unmanaged memory. For the managed memory, however, I already discovered the following:
StdIOTransport
and some reflection code.StdIOTransport
is instantiated inawait Playwright.CreateAsync()
In the following screenshot, you see the increasing memory footprint over time:
In the next screenshot, you see the dominating types retaining the memory (last memory dump taken):
And last but not least, the following screenshot shows several issues, e.g. duplicate strings, sparse arrays, and leaking event handlers:
Environment
The text was updated successfully, but these errors were encountered: