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

+2 -2
View File
@@ -1,5 +1,6 @@
import { Application } from "@nativescript/core";
import { fetchWithUserAgent } from "../../util/http";
import { copyToJavaByteBuffer } from "../../native/java-direct-buffer";
declare const android: any;
declare const androidx: any;
@@ -93,8 +94,7 @@ async function downloadAndOpenPdf(url: string, appName: string, activity: any):
const file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`);
const stream = new java.io.FileOutputStream(file);
try {
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
stream.getChannel().write(buffer);
stream.getChannel().write(copyToJavaByteBuffer(bytes));
} finally {
stream.close();
}
+5 -1
View File
@@ -138,6 +138,7 @@ export class FaceclawCommunicatorBridge {
// immediately before the synchronous Java call that consumes them.
private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480);
private readonly frameDrawsBuffer = new JavaDirectBuffer();
private readonly shellSceneBuffer = 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
@@ -545,7 +546,10 @@ export class FaceclawCommunicatorBridge {
*/
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
const snapshot = new Uint8Array(bytes);
await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true);
await this.enqueueJavaCall(
() => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId),
true,
);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
+4 -4
View File
@@ -5,6 +5,7 @@
* reasonable for a sideloaded personal tool).
*/
import { Utils } from "@nativescript/core";
import { copyToJavaByteBuffer } from "./java-direct-buffer";
declare const android: any;
declare const java: any;
@@ -150,10 +151,9 @@ export function writeBinaryFile(path: string, bytes: Uint8Array): boolean {
if (parent) parent.mkdirs();
const stream = new java.io.FileOutputStream(file);
try {
// An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion
// submitSurfaceFrame relies on), so write through the channel.
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
stream.getChannel().write(buffer);
// Write through the channel from a Java direct buffer: a bulk copy, and
// passing the ArrayBuffer itself would leak it (java-direct-buffer.ts).
stream.getChannel().write(copyToJavaByteBuffer(bytes));
} finally {
stream.close();
}
+7 -2
View File
@@ -1,3 +1,8 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(buffer)) }
export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, buffer) }
// The image crosses as a fresh Java buffer: passing a JS ArrayBuffer to Java
// leaks it (java-direct-buffer.ts).
export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(copyToJavaByteBuffer(new Uint8Array(buffer)))) }
export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, copyToJavaByteBuffer(new Uint8Array(buffer))) }
+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,
+3 -1
View File
@@ -1,7 +1,9 @@
import { grayImageFromPacket } from './image-files'
import { type GrayImage } from '../graphics/image'
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
/** The encoded bytes cross as a fresh Java buffer; passing the ArrayBuffer leaks it (java-direct-buffer.ts). */
export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null {
return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, width, height))
return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(copyToJavaByteBuffer(new Uint8Array(bytes)), width, height))
}
+17
View File
@@ -93,3 +93,20 @@ export class JavaDirectBuffer {
return this.load(new Uint8Array(buffer));
}
}
/**
* Copy `bytes` into a fresh Java direct buffer (position 0, limit
* `bytes.length`) for a one-off crossing such as an atlas registration or a
* file write. Unlike a JavaDirectBuffer nothing is pinned between calls: the
* Java buffer is an ordinary object, freed once the JS side drops it.
*/
export function copyToJavaByteBuffer(
bytes: Uint8Array,
backend: DirectBufferBackend = nativeScriptBackend,
): JavaByteBufferLike {
// A zero-capacity direct buffer may have no address to view.
const javaBuffer = backend.allocateDirect(Math.max(bytes.length, 1));
backend.viewOf(javaBuffer).set(bytes);
javaBuffer.limit(bytes.length);
return javaBuffer;
}
+5 -3
View File
@@ -1,3 +1,5 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */
@@ -10,11 +12,11 @@ export function runKotlinProtocolSmokeTest(): void {
const compositor = new native.SurfaceCompositor()
compositor.configureScreen(3, 2)
compositor.configureSurface('test', 0, 0, 3, 2, 0, 0)
const pixels = new Uint8Array([0, 16, 255, 32, 48, 64])
compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1')
const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64]))
compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1')
assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer')
assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing')
const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer))
const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels))
assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android')
}
+2 -1
View File
@@ -54,6 +54,7 @@ export class PreviewDisplayTarget implements DisplayTarget {
// (java-direct-buffer.ts).
private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480);
private readonly frameDrawsBuffer = new JavaDirectBuffer();
private readonly shellSceneBuffer = new JavaDirectBuffer();
constructor() {
const context = Utils.android.getApplicationContext();
@@ -109,7 +110,7 @@ export class PreviewDisplayTarget implements DisplayTarget {
}
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId);
this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
+8 -4
View File
@@ -1,17 +1,21 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
// Byte payloads cross as fresh Java buffers: passing a JS ArrayBuffer to Java
// leaks it (java-direct-buffer.ts).
/** Android bridge to the process-wide shared Kotlin atlases. */
export function textureAtlasAvailable(): boolean { return !!global.isAndroid }
export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) }
export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void {
const reader = new com.faceclaw.app.AndroidByteReader(buffer)
const reader = new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer)))
if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader)
else com.faceclaw.app.GlyphAtlas.register(reader)
}
export function registerFirmwareGlyphs(buffer: ArrayBuffer): void {
com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer))
com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer))))
}
export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number {
const buffer = pixels.byteOffset === 0 && pixels.byteLength === pixels.buffer.byteLength ? pixels.buffer : pixels.slice().buffer
return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(buffer))
return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(pixels)))
}
+1 -1
View File
@@ -148,7 +148,7 @@ test('heading updates reuse the texture; brightness, layout and clipping changes
test('the background registers once and uses a nine-byte image reference on later frames', () => {
const registered = [];
const atlas = load('app/native/texture-atlas.ts', {}, '', {
const atlas = load('app/native/texture-atlas.ts', { './java-direct-buffer': { copyToJavaByteBuffer: (bytes) => bytes } }, '', {
global: { isAndroid: true },
com: { faceclaw: { app: { AndroidByteReader: class { constructor(buffer) { this.buffer = buffer; } }, ImageAtlas: { ensure: (...args) => {
registered.push(args);
+12 -1
View File
@@ -1,7 +1,7 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const { JavaDirectBuffer } = require("../.test-build/app/native/java-direct-buffer.js");
const { JavaDirectBuffer, copyToJavaByteBuffer } = require("../.test-build/app/native/java-direct-buffer.js");
/**
* A stand-in for java.nio.ByteBuffer.allocateDirect: fixed capacity, a
@@ -109,3 +109,14 @@ test("a view on a larger buffer contributes only its own bytes", () => {
const view = whole.subarray(10, 30);
assert.deepEqual(consume(pool.load(view), 20), whole.slice(10, 30));
});
test("copyToJavaByteBuffer gives each crossing its own exact-length buffer", () => {
const backend = fakeBackend();
const whole = frame(100, 5);
const first = copyToJavaByteBuffer(whole.subarray(10, 30), backend);
const second = copyToJavaByteBuffer(frame(7, 1), backend);
assert.notEqual(first, second);
assert.deepEqual(consume(first, 20), whole.slice(10, 30));
assert.deepEqual(consume(second, 7), frame(7, 1));
assert.equal(consume(copyToJavaByteBuffer(new Uint8Array(0), backend), 0).length, 0);
});