fix(linux): avoid memory leak from unnecessary encoder re-probing - #5404
Conversation
On Linux, needs_encoder_reenumeration() unconditionally returned true, causing full encoder re-probing (h264, hevc, av1) on every client reconnect. Each probe cycle allocates ~20 MB of FFmpeg CBS buffers for HEVC/AV1 VPS validation that are not fully reclaimed, leading to unbounded memory growth. Fix by tracking the render device path and only re-probing when it actually changes (e.g. GPU hotplug or driver reload).
|
Bundle ReportBundle size has no change ✅ |
|
This PR improves things for me but I'm still seeing a +8MB leak on each capture re-init when using Vulkan (on CachyOS latest). |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5404 +/- ##
==========================================
- Coverage 26.71% 26.70% -0.01%
==========================================
Files 111 111
Lines 25324 25329 +5
Branches 11194 11198 +4
==========================================
- Hits 6766 6765 -1
+ Misses 15538 15333 -205
- Partials 3020 3231 +211
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 53 files with indirect coverage changes Continue to review full report in Codecov by Harness.
|
|
@Kishi85 thank you for the tests, you might try to build with FFmpeg CBS fix too here LizardByte/build-deps#727 . It requires the custom build of FFmpeg with the patch file: and build sunshine with option |
Unfortunately still seeing the 8MB leak per re-init (tested with display switch) with #5407 and this one applied. No leak with VAAPI so it's definitely a Vulkan specific issue. |
Hhhm, I can't reproduce it after FFmpeg/sunshine fixes. I checked with clients' reconnect, changing monitors, changing resolutions. Are you sure the compiled FFmpeg with fixes is used? |
According to my CMake output it's using v2026.713.1051 which should include your patch as listed in #5407. I'm also seeing 2 leaking VkImageView when using the ffmpeg command compared to the single leaking VkImageView in #5360 (comment) maybe that's a hint to what's going on but I don't even have a clue how I'd debug this. |
|
@Kishi85 does it help? LizardByte/build-deps#728 |
That seems to fix it. Running this I'm getting a very stable 128MB memory usage when running with Vulkan. |



Description
On Linux,
needs_encoder_reenumeration()unconditionally returnstrue, causing full encoder re-probing (h264, hevc, av1) on every client reconnect. Each probe cycle triggers FFmpeg CBS (Coded Bitstream) operations that allocate ~7.9 MB per HEVC VPS clone for validation. With 2 HEVC + 2 AV1 probes per reconnect, this leaks ~20 MB per cycle that is never fully reclaimed.Fix by caching the render device path and only returning
truewhen the GPU actually changes (hotplug, driver reload). This reduces per-reconnect memory growth from ~20 MB to <1 MB.Measured results (AMD RX 9070 XT, RADV, Vulkan encoder):
The residual ~0.4-0.6 MB is malloc arena fragmentation (confirmed reclaimable via
malloc_trim).Root cause analysis
needs_encoder_reenumeration()always returnedtrueon Linuxprobe_encoders()is called fromnvhttp.cppon every launch/resumecbs_h265_replace_vpswhich clonesH265RawVPS(7.9 MB struct) viaav_refstructIssues Fixed or Closed
Screenshot
Type of Change
Checklist
AI Usage