Skip to content

fix: free a hidden plugin's graphics devices while its panel isn't showing - #43

Merged
CodySimonds65 merged 2 commits into
mainfrom
feat/serene-kepler-1df1a9
Oct 7, 2026
Merged

CodySimonds65 merged 2 commits into
mainfrom
feat/serene-kepler-1df1a9

Conversation

@CodySimonds65

Copy link
Copy Markdown
Owner

What

A community plugin whose panel isn't showing now costs the app process under 1 MB instead of about 80 MB. Plugins keep running while hidden: they still receive events and update their cards, and page timers run at full speed.

Why

Every WebView2CompositionControl builds a Direct3D image of its own to copy its page's frames into WPF: a D3D11 device, a D3D9Ex device and a capture session, whatever the control's size. In the app process that is about 80 MB private, 66 threads and 1,700 handles per view, including the 2x2 invisible views of parked plugins. With 6-7 plugins the app sat above 1.2 GB and Windows logged a RADAR_PRE_LEAK_64 warning.

How

In WebPlugin, a parked view disposes that image and builds it again when its panel is shown.

  • The release is followed by a GC.Collect(). Without it the devices and their threads stay alive: 12 released views still held 405 threads a minute later.
  • It reaches into the control's private members, like UnhookWindowClose. If a WebView2 SDK update renames them, the plugin refuses to start, so the lost saving is noticed at once.
  • If rebuilding the image fails when a panel is shown (a graphics driver restart, say), the plugin stops with its Reload button instead of the exception ending the app.

Options measured

Standalone WPF harness, 6 views each. The empty window is 150 MB / 24 threads / 2,099 handles.

Option Private MB Threads Handles Page while hidden
Current (composition control, parked 2x2) 577-608 410 12,033 full speed
CoreWebView2Controller.IsVisible = false 606 410 12,061 timers cut to 1/s, requestAnimationFrame stops
MemoryUsageTargetLevel = Low 609 410 12,073 full speed
Windowless controller, no visual 155 26 2,178 full speed
Composition control with its capture released (this PR) 150 26 2,207 full speed

IsVisible = false and the low memory target save nothing in the app process. The windowless controller saves the same amount, but showing a panel would mean rewriting the control's rendering and input handling.

Before and after

7 plugins hosted by the real WebPlugin class in a harness window with a temp data folder.

Phase Before After
No plugins 146 MB / 20 threads / 2,093 handles 142 MB / 20 / 2,093
7 plugins, all hidden 663 MB / 481 / 13,808 143 MB / 33 / 2,328
One panel shown 671 MB / 481 / 13,812 236 MB / 97 / 3,978
All hidden again 676 MB / 482 / 13,827 152 MB / 34 / 2,343

In every phase the slowest plugin's card updated at the full 2 events per second sent, and page timers ran at 20 per second. Screenshots confirmed a panel paints correctly after being hidden, including a page that wasn't changing. Hit-testing still reaches the web view; a real click was not sent.

Costs

  • Showing a panel takes about 70 ms to rebuild the capture (measured in the standalone harness).
  • Each hide-and-show leaks roughly 0.4 MB, because the D3D9Ex device does not fully free. 30 cycles added 13 MB. Delaying the release until a panel has been hidden for a while would cut the churn; that is left out.
  • Game panels are unchanged. They are on screen, so they still pay the full cost each.

Tests

None added: nothing here meets the real-damage bar. Core (323) and Desktop (37) suites pass. The 8 Leaderboard store tests need Docker, which wasn't running locally.

…owing

Each WebView2CompositionControl builds a Direct3D image of its own to copy
its page's frames into WPF: a D3D11 device, a D3D9Ex device and a capture
session. That costs this process about 80 MB, 66 threads and 1,700 handles
for every view, including the 2x2 invisible ones of parked plugins.

A parked plugin's view now disposes that image and builds it again when its
panel is shown. The page keeps running, visible to itself, at full timer
speed.

Measured with 7 plugins hosted by the real WebPlugin, all parked:
  before  663 MB private, 481 threads, 13,808 handles
  after   143 MB private,  33 threads,  2,328 handles
(the same window with no plugins: 146 MB, 20 threads, 2,093 handles)
@CodySimonds65
CodySimonds65 merged commit 4b9de74 into main Oct 7, 2026
CodySimonds65 added a commit that referenced this pull request Oct 7, 2026
fix: free a hidden plugin's graphics devices while its panel isn't showing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant