Stop leaking every submitted frame's pixel buffer in native memory

Passing a JS ArrayBuffer to a Java ByteBuffer parameter leaks it in
@nativescript/android 9.0.4 through 9.1.1: JsArgConverter wraps the backing store with
NewDirectByteBuffer and takes a JNI global ref that it never deletes. The
ref keeps the ByteBuffer reachable, so makeInstanceWeakAndCheckIfAlive
always answers "alive", the JS finalizer re-arms, and V8 never frees the
ArrayBuffer. submitSurfaceFrame passed a fresh copy of each frame this way,
so every frame leaked a full surface (576x260, 640x452 or 640x480 bytes)
plus one global ref, including frames that Java then discards as unchanged.
The draw-list buffer and buzzer payloads leaked the same way.

Each payload now goes through a reused Java direct ByteBuffer
(app/native/java-direct-buffer.ts): JS writes into it through an
ArrayBuffer.from view, and the Java object itself is passed, which takes
NativeScript's ordinary object path. Every consumer (SurfaceCompositor
applyAndComposite, playBuzzerSequence) copies before returning, so one
buffer per JS context is safe.

Covered: the communicator bridge (frames, draw lists, buzzer, which also
carries EvenHub tones), the preview-only display, and all eight worker
apps. Workers submit through the Android getActiveDisplay() (frames) and
worker-buzzer (tones), so the reused buffers live there rather than in
each worker; the worker sources are shared with iOS, whose active display
takes the ArrayBuffer itself. The
smaller one-off crossings (glyph and image atlas registration, the gray
preview helper, firmware and file writes) are left as they are.

tests/java-direct-buffer.test.cjs covers the helper against a fake
ByteBuffer, and the Android worker-buzzer test stubs it.

(Rebased onto main by James Babcock: worker changes moved into
active-display.ts and worker-buzzer.ts.)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris LeeandClaude Opus 5 authored and James Babcock committed 2026-09-25 22:24:22 -07:00
1 parent 833f9d97f0
commit 54eae0c6b9
8 files changed
+283 -17

No files matched your search

+15 -6
View File
@@ -1,6 +1,7 @@
import type { RingInput } from "../g2/ring-input";
import { ImageSource, Utils } from "@nativescript/core";
import * as frameTimings from "./frame-timings";
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any;
@@ -132,6 +133,12 @@ export class FaceclawCommunicatorBridge {
private javaCallQueue: Promise<void> = Promise.resolve();
/** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */
private queuedJavaCalls = 0;
// Reused Java-side buffers for byte payloads; passing a JS ArrayBuffer to
// Java leaks it (see java-direct-buffer.ts). Loaded inside the queued call,
// immediately before the synchronous Java call that consumes them.
private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480);
private readonly frameDrawsBuffer = new JavaDirectBuffer();
private readonly buzzerBuffer = new JavaDirectBuffer();
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
// Recent frame-finished outcomes from the Java side, so waitForFrameFinished
// does not race against finishes that land before the wait starts.
@@ -594,15 +601,17 @@ export class FaceclawCommunicatorBridge {
*/
glyphs: ArrayBuffer | null = null,
): Promise<void> {
// Snapshot because the Java call is deferred; the buffer is passed as an
// ArrayBuffer, which NativeScript marshals to a ByteBuffer without the
// ~150ms per-element copy a byte[] parameter would need.
// Snapshot because the Java call is deferred. The bytes reach Java as a
// ByteBuffer (no ~150ms per-element copy, as a byte[] parameter would
// need), but through a reused Java direct buffer rather than by passing
// the ArrayBuffer itself, which NativeScript never frees
// (java-direct-buffer.ts).
const snapshot = new Uint8Array(pixels8bpp);
// inlineWhenIdle: this is the frame path, the Java side of it measures
// ~5ms (composite + pack), and the caller awaits it either way.
await this.enqueueJavaCall(() => {
this.communicator.submitSurfaceFrame(
snapshot.buffer,
this.framePixelsBuffer.load(snapshot),
surfaceId,
Math.round(rect.x),
Math.round(rect.y),
@@ -611,7 +620,7 @@ export class FaceclawCommunicatorBridge {
fingerprint,
Math.round(nonNegativeNumber(paintMs)),
Math.round(nonNegativeNumber(frameId)),
glyphs,
this.frameDrawsBuffer.loadOptional(glyphs),
);
}, true);
}
@@ -673,7 +682,7 @@ export class FaceclawCommunicatorBridge {
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
const snapshot = new Uint8Array(payload);
await this.enqueueJavaCall(() => {
this.communicator.playBuzzerSequence(snapshot.buffer);
this.communicator.playBuzzerSequence(this.buzzerBuffer.load(snapshot));
});
}