Skip to content

[Bug]: Playwright running in .NET hosted services seems to leak memory #2962

@mu88

Description

@mu88

Version

1.45.0

Steps to reproduce

  1. Clone my repro repo: https://github.com/mu88/Repro_Playwright
  2. Run dotnet build
  3. Run pwsh Web\bin\Debug\net8.0\playwright.ps1 install
  4. Run the app (either via IDE or dotnet run)
  5. Check the app's log output whether a screenshot is created every 15 s (log message info: NewScreenshotCreator[0] Screenshot created)
  6. Attach a memory profiler to the app (e.g. dotMemory)
  7. Create a first memory dump
  8. Wait some time, e.g. 15 min
  9. Create another memory dump

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:

  • Most of the memory is retained by StdIOTransport and some reflection code.
  • StdIOTransport is instantiated in await Playwright.CreateAsync()

In the following screenshot, you see the increasing memory footprint over time:

2024-07-17_11h06_15

In the next screenshot, you see the dominating types retaining the memory (last memory dump taken):

2024-07-17_11h06_55

And last but not least, the following screenshot shows several issues, e.g. duplicate strings, sparse arrays, and leaking event handlers:

2024-07-17_11h07_42

Environment

- Operating System: Windows 10, Windows 11, WSL2, Linux (Raspberry Pi)
- CPU: arm64, amd64
- Browser: All
- .NET Version (TFM): [net8.0]

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions