BrowserContext resource reclamation in high-volume test cycles
27K reputation · 21 Sept 2020, 16:36 UTC
BrowserContext Memory Management
Playwright provides BrowserContext to isolate test environments without the overhead of restarting the entire browser process. In large-scale test suites where hundreds of contexts are created and destroyed sequentially, the goal is to ensure the browser process memory footprint remains stable.
While browserContext.close() is used to release session resources, there is uncertainty regarding the efficiency of memory reclamation for the underlying browser process after repeated cycles of context creation and destruction.
If pages are explicitly closed before the context is terminated, it is unclear if the browser process returns to its baseline resident set size (RSS) or if a gradual increase in heap usage persists over time.
- Does the browser process fully reclaim memory allocated to a
BrowserContextonceclose()is called? - Is there a documented threshold for the number of context cycles before a browser restart is required to prevent memory exhaustion?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
27,025 reputation · 22 Sept 2020, 02:09 UTC
To build on the discussion of RSS drift, it is important to distinguish between the memory managed by the browser process and the memory managed by the test runner's language wrapper (e.g., Node.js or Python). While await context.close() signals the browser to destroy the incognito profile and associated pages, the JavaScript or Python object representing that context remains in the runner's heap until garbage collected.
In extremely high-volume cycles, if the test runner's GC does not trigger frequently enough, you may see a secondary memory climb in the runner process, even if the browser process is stable. To verify if this is occurring, you can monitor the resident set size (RSS) of both the browser and the test runner separately using system tools like htop or Activity Monitor. If the runner's memory grows while the browser's plateaus, the issue is likely object retention in the test suite rather than a browser-side leak.