Repository navigation
fix(ios): drain autoreleased Metal objects on the offscreen worker - #19
Merged
riteshshukla04 merged 7 commits intoNov 4, 2026
Merged
Conversation
|
@mickeypause is attempting to deploy a commit to the Margelo Team on Vercel. A member of the Team first needs to authorize it. |
Member
|
Can you rebase your PR with main |
Bound Metal temporaries to each worker drain, including final surface cleanup. Keep the Android C++ path unchanged. 450 filtered renders: RSS growth +51.6 MiB before, -3.7 MiB after (30 MiB gate; Debug iOS Simulator).
Discard earlier-process RSS samples after Harness restarts the app. Assert blur blends an interior boundary; reject a zero-blur control. Add regression checks for process changes and insufficient samples.
Restore contributor docs, runner configuration, and Android leak tooling. Preserve iOS measurement tools on validation/ios-offscreen-memory. Keep the offscreen workload and its combined Jest exclusion list.
mickeypause
force-pushed
the
fix/ios-offscreen-autorelease-pool
branch
from
November 3, 2026 16:34
d8b5c58 to
3313b99
Compare
Contributor
Author
Done |
Member
|
Can you also delete the harness file and adjust it in existing one. Lets not add new files for everything |
Contributor
Author
Updated |
Member
|
I found same issue with images. So I fixed it here |
Hold the pool in OffscreenContext instead of around the worker loop.
renderToImage, encode and save take the offscreen device on Nitro's
promise threads, which never drain a pool either. Every offscreen GPU
call already runs inside an OffscreenContext, so one RAII member covers
the worker, those calls and surface teardown.
450 iterations on the iOS 27 simulator:
- offscreen leak soak: +2.5 MiB
- renderToImage: +2.6 MiB (was +83.5 MiB)
- encode('png'): flat (was +50.9 MiB)
Member
|
Thanks for fixing this :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Idea behind this PR
I encountered this issue while porting my drawing app from Skia to tgfx. The app lets users draw on a canvas, but repeated offscreen rendering caused a massive increase in RAM usage, even when the rendered content wasn't visible on screen.
The offscreen worker wasn't draining autoreleased temporary objects between render passes. This PR adds an autorelease pool around each Apple offscreen worker pass so those objects are released correctly.
<------- Anything below here was generated by AI. ------->
Problem and fix
The process-wide offscreen worker is a detached C++ thread. Repeated filtered renders can accumulate autoreleased Apple objects despite callers disposing input images and snapshots.
On Apple, run each worker pass inside
@autoreleasepool, including acquisition and destruction of its temporary strong surface reference. Android keeps the existing direct C++ call. This addresses temporary-object lifetime, not visibility culling.Scope
Four files, +89/−0 lines:
offscreen.harness.tsx, with upload, moving-rectangle and blur-boundary pixel assertions. It runs with the normal suite; no separate Harness file, custom flag or default name filter.No public API, wire format, engine pin, Swift, Android implementation, podspec, contribution-guide or host-sampler changes.
Reproduce and validate
Configure the existing
ios-simrunner for your simulator and install a matching Debug example build:This command checks pixels, not memory. The external, PID-aware RSS checker and its regression tests live on a separate validation commit; see its reproduction commands. On this branch, pass
--test src/offscreen.harness.tsx; that runs the regular offscreen checks together with the soak.Two fresh-process iOS 27 Simulator Debug pairs completed all 450 renders:
The external gate compares settled medians against 30 MiB. Both workers pass pixel assertions; bypassing blur fails the boundary assertion and cannot produce a passing memory verdict. RSS is process accounting, not GPU-only memory.
Additional recorded coverage:
SceneTestpasses its assertions but aborts during shaderc/glslang shutdown on both workers;SnapshotTestandAllocBenchexit 0. Existing example/benchmark tooling failures and missing approved image baselines remain. The whole repository is not green.Detailed results, controls and limitations are in our fork review PR. Device startup/signing adjustments used during validation were local and restored.
The branch is now rebased onto upstream
bcf2223(including merged PR #18), with rebased fix revision3313b99. The two configuration conflicts were resolved by preserving upstream's runner settings and addingoffscreenLeakSoakto its existing Jest exclusion list. The native fix merged without conflicts. After rebase, 406 core tests, core lint and typecheck pass. Earlier device and memory results above targetd8b5c58, based on alpha.12 (adee18e); native/device builds and the memory regression have not yet been rerun against the updated engine.The test consolidation is a single follow-up commit,
642d0a5, atop the rebased fix. Its tree is identical to the locally validated consolidation: 406 core tests pass; core lint/typecheck and example lint pass; the example's eight type errors match the original committed sources. Jest CLI configuration confirms no default name filter, with standard--testNamePatternavailable for a focused run. Native/device and memory checks have not been rerun after this move.App demonstration
Before/after MP4 and capture details document the original investigation in a separate drawing app on tgfx alpha.8. The clip starts with memory already accumulated and reports UI callback cadence, not hardware presentation FPS. It is supporting demonstration, separate from the alpha.12 library regression above.