Stop leaking the remaining JS ArrayBuffers passed to Java

The same NativeScript conversion leak as the frame path (see
java-direct-buffer.ts) applied to every other ArrayBuffer handed to a
Java ByteBuffer parameter:

- Shell scenes (submitShellScene on the bridge and the preview display),
  submitted on every shell render, which the frame-path fix predates.
  These now go through a reused JavaDirectBuffer like frames do.
- Glyph, firmware-glyph and image atlas registration, Mapbox image
  decode, the preview placeholder bitmap, firmware hash/write, binary
  file writes and the EvenHub privacy-policy download, plus the Kotlin
  protocol smoke test. These are rare or large, so they cross through a
  new copyToJavaByteBuffer: a fresh Java direct buffer per call that is
  freed normally, rather than one pinned for the life of the isolate.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
James BabcockandClaude Opus 5.5 committed 2026-09-25 22:28:45 -07:00
1 parent 8cdee83ff4
commit 1812818ae2
12 files changed
+72 -25

No files matched your search

+6 -5
View File
@@ -1,16 +1,17 @@
import { ImageSource } from "@nativescript/core";
import { type GrayImage } from "../graphics/image";
import { copyToJavaByteBuffer } from "./java-direct-buffer";
declare const com: any;
const PREVIEW_BRIGHTEN_GAMMA = 0.7;
/**
* Build the phone-UI preview for a frame. The pixel buffer is passed to Java
* as an ArrayBuffer (marshalled to a ByteBuffer) and expanded to ARGB there;
* doing the per-pixel work in JS or copying element-by-element across the
* bridge costs ~150ms per frame.
* Build the phone-UI preview for a frame. The pixels are copied into a Java
* direct ByteBuffer and expanded to ARGB there; doing the per-pixel work in JS
* or copying element-by-element across the bridge costs ~150ms per frame, and
* passing the ArrayBuffer itself would leak it (java-direct-buffer.ts).
*/
export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
if (!global.isAndroid) {
@@ -19,7 +20,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
const baked = image.withDrawsBaked();
const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray(
baked.pixels.buffer,
copyToJavaByteBuffer(baked.pixels),
baked.width,
baked.height,
PREVIEW_BRIGHTEN_GAMMA,