microsoft / microsoft/playwright-dotnet

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

Open
#2,962 13 comments 3 reactions 1 assignee View on GitHub

@mxschmitt is already working on this.

Since Sep 23, 2024.

P3-collecting-feedback
Dominant language
C#
Stars
3k
Forks
304
Avg merge
20h 47m
Merged PRs (30d)
6

Description

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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.