Repository navigation
Description
Have I written custom code (as opposed to using a stock example script provided in MediaPipe)
Yes
OS Platform and Distribution
macOS 15 (Chromium-based browsers)
Browser and version if the issue happens on browser
Chrome 152
Programming Language and version
JavaScript / TypeScript
MediaPipe version
@mediapipe/tasks-vision 1.0.1 (latest stable as of 2026-08-30; wasm assets from the npm package, hash-verified)
Task name (e.g. Image classification, Gesture recognition etc.)
ImageSegmenter (selfie_segmenter.tflite), runningMode: "VIDEO", delegate: "GPU", outputConfidenceMasks: true
Describe the actual behavior
When segmentForVideo() is called continuously (~24fps) in a long-running video call (background replacement use case), memory grows without bound. Measured over a 75-minute session, via V8 heap snapshots and retainer analysis:
- The Emscripten GL handle tables (
GL.framebuffers,GL.textures,GL.syncs) insidevision_wasm_internal.jsgrow to 11.19 MB each (~1.4M slots per array). The retainer chain goes through thesetWindowedCanvasSizeclosure scope of the wasm module. - Renderer process RSS grows monotonically at ~1.2 MB/min, while a control tab without the segmenter stays flat.
- After roughly 60 minutes, GC pauses / long tasks grow enough that users notice the page becoming sluggish (this matched a real-world user report that triggered our investigation).
Reproduced with both API styles at the same rate (~0.5 MB per handle table per minute):
- return-value style
const result = segmentForVideo(video, ts)withmask.close()called every frame - callback style
segmentForVideo(video, ts, callback)without retaining the result
Additional observations:
delegate: "CPU"does not exhibit the growth (but halves our frame rate, so it is not a viable workaround).- Recreating the ImageSegmenter and calling
close()on the old instance resets the tables — this is our current mitigation (recreate every 5 minutes).
I have not determined whether the underlying WebGL objects are actually left undeleted or whether they are deleted but the handle IDs are never recycled (so only the JS-side tables grow); either way the unbounded growth originates inside the task's GPU-delegate path and degrades multi-hour sessions.
Describe the expected behaviour
GL handle tables / per-frame WebGL resources should stay bounded during continuous segmentForVideo use, so long video sessions do not degrade.
Standalone code/steps to reproduce the issue
const fileset = await FilesetResolver.forVisionTasks(wasmPath);
const segmenter = await ImageSegmenter.createFromOptions(fileset, {
baseOptions: { modelAssetPath: "selfie_segmenter.tflite", delegate: "GPU" },
runningMode: "VIDEO",
outputConfidenceMasks: true,
});
const render = () => {
segmenter.segmentForVideo(videoEl, performance.now(), (result) => {
// read result.confidenceMasks[0], composite onto a canvas, retain nothing
});
videoEl.requestVideoFrameCallback(render);
};
render();
// Take a Chrome heap snapshot after 10+ minutes: three "(object elements)"
// arrays retained via GL.framebuffers / GL.textures / GL.syncs grow ~0.5 MB/min each.Other info / Complete Logs
Happy to share the heap-snapshot analysis scripts and raw retainer chains if useful.
Activity
akashvverma1995 commentedon Sep 1, 2026
Hi @Ryosuke-Kimoto,
Thank you for bringing this issue to our attention. We will look into it and get back to you with an update as soon as possible.
akashvverma1995 commentedon Sep 22, 2026
Hi @Ryosuke-Kimoto
It would be very helpful if you could share your heap-snapshot analysis scripts or logs so we can take a deeper look.
Immediate Workaround:
Could you try explicitly calling
.close()on the mask object before your callback ends? This forces the native WebGL texture to free immediately.Here is how you can update your code to test this:
Please give this workaround a try and let us know if it stops the memory growth on your end!
github-actions commentedon Sep 30, 2026
This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.
Ryosuke-Kimoto commentedon Oct 3, 2026
Thanks for the suggestion! I tested the workaround today — unfortunately explicit
mask.close()inside the callback makes no difference.A/B measurement (same build, only difference is the
mask.close()call)Method: headless Chrome drives
segmentForVideoat ~24fps (callback style, GPU delegate, tasks-vision 1.0.1 — still the latest on npm as of today). Two V8 heap snapshots are taken via CDP (after a 30s warmup, then 3 minutes later), and I measure the growth of large(object elements)backing stores, which the retainer analysis below identifies as the Emscripten GL handle tables.mask.close()before callback returns (your suggestion)For reference, our original implementation used the return-value style (
const result = segmentForVideo(video, ts)) and already calledmask.close()every frame — it leaked at the same per-call rate. So the growth appears to happen inside the task's GPU-delegate path during inference itself, regardless of the mask object's lifecycle on the JS side.The tables never shrink; growth is strictly monotonic until the ImageSegmenter instance is
close()d entirely (recreating the instance resets the tables — that's our current production mitigation, recreate every 5 minutes).Retainer chains (from a Chrome heap snapshot after a 60-min session; tables at 11.19 MB each)
i.e.
GL.framebuffers/GL.syncs/GL.texturesinvision_wasm_internal.js.Analysis scripts
As requested, the two scripts I used are attached below. Both operate on plain
.heapsnapshotfiles (saved from Chrome DevTools or CDPHeapProfiler.takeHeapSnapshot); nothing app-specific is needed to reproduce — any page drivingsegmentForVideoat video rate for a few minutes shows the growth.heap-diff.mjs — flat aggregation by node type/name (usage: node heap-diff.mjs before.heapsnapshot after.heapsnapshot out.md)
heap-retainers.mjs — retainer-chain walker for large arrays (usage: node heap-retainers.mjs snapshot.heapsnapshot out.md)
Happy to run further experiments if it helps — this is easy for me to reproduce.
akashvverma1995 commentedon Oct 7, 2026
Hi @Ryosuke-Kimoto
Thank you for providing the details. I'll look into this and get back to you with an updates.