Skip to content

Add API to retrieve WASM/JS memory usage#58

Merged
keiji merged 6 commits intomainfrom
feature/memory-usage-api-8363786151944416957
Jan 2, 2026
Merged

Add API to retrieve WASM/JS memory usage#58
keiji merged 6 commits intomainfrom
feature/memory-usage-api-8363786151944416957

Conversation

@google-labs-jules
Copy link
Contributor

Added getMemoryUsage() API to Jp2kDecoder and Jp2kDecoderAsync to allow applications to monitor WASM and JS heap usage. This involved injecting a helper function into the JavaScript sandbox and parsing the result. Also fixed a constructor mismatch in Jp2kDecoderTest.


PR created automatically by Jules for task 8363786151944416957 started by @keiji

This commit introduces a new API to retrieve memory usage statistics
from the underlying JavaScript/WASM engine.

Key changes:
- Created `MemoryUsage` data class to hold heap statistics.
- Updated `Jp2kDecoderAsync` to inject a `getMemoryUsage` JS function
  into the sandbox, which queries `wasmInstance.exports.memory` and
  `performance.memory`.
- Added `getMemoryUsage()` method to `Jp2kDecoderAsync` and
  `Jp2kDecoder` to expose this data to the application layer.
- Updated `Jp2kDecoderTest` to verify the new API and fixed incorrect
  constructor usage in the test.
@google-labs-jules
Copy link
Contributor Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

google-labs-jules bot and others added 5 commits January 2, 2026 15:38
Updates the sample app `MainActivity` to utilize the newly added
`getMemoryUsage()` API.

Key changes:
- Calls `decoder.getMemoryUsage()` after decoding the image.
- Displays the WASM and JS heap statistics in the UI.
- Imports `MemoryUsage` and necessary Compose UI components.
This commit updates the KDoc for `MemoryUsage` to explicitly state that
JS heap statistics (`jsHeapSizeLimit`, `totalJSHeapSize`, `usedJSHeapSize`)
may be null if the underlying `performance.memory` or `console.memory` APIs
are restricted or unavailable in the Android `JavaScriptEngine` environment.

Also added a fallback to check `console.memory` in `Jp2kDecoderAsync`
to increase the chance of retrieving memory stats on supporting environments.
This commit updates `MemoryUsage` documentation to cite MDN as the source
for the non-standard `performance.memory` API, fulfilling the requirement
to document the origin of these values and explain their potential
unavailability in the Android `JavaScriptEngine` sandbox.
This commit modifies the `getMemoryUsage` API to remove JS heap statistics
as they are generally unavailable in the Android `JavaScriptEngine` sandbox.
It focuses solely on `wasmHeapSizeBytes`.

Also updates the `MainActivity` sample to retrieve and display the
WASM heap size before and after decoding an image, allowing users to
visualize memory consumption changes.

Changes:
- Removed JS heap fields from `MemoryUsage`.
- Simplified `getMemoryUsage` JS injection in `Jp2kDecoderAsync`.
- Updated `MainActivity` to show "Before -> After" heap size.
@keiji keiji marked this pull request as ready for review January 2, 2026 16:09
@keiji keiji merged commit 9c0d8e5 into main Jan 2, 2026
2 checks passed
@keiji keiji deleted the feature/memory-usage-api-8363786151944416957 branch January 2, 2026 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments