Title: fix: stop leaking a JNI global ref per ArrayBuffer passed to Java by dpwhittaker · Pull Request #2062 · NativeScript/android · GitHub
Open Graph Title: fix: stop leaking a JNI global ref per ArrayBuffer passed to Java by dpwhittaker · Pull Request #2062 · NativeScript/android
X Title: fix: stop leaking a JNI global ref per ArrayBuffer passed to Java by dpwhittaker · Pull Request #2062 · NativeScript/android
Description: Description When a JS ArrayBuffer, SharedArrayBuffer or typed array crosses to Java for the first time, JsArgConverter::ConvertArg wraps it in a direct ByteBuffer, then calls env.NewGlobalRef(buffer) and never deletes the result. JsArgToArrayConverter::ConvertArg has the same code, and is used for Java array elements, overload resolution and values returned to Java. GetOrCreateObjectId already registers the buffer in the runtime's strong instances for as long as the JS object lives, so the extra reference does nothing except pin the buffer. Two consequences: Every distinct buffer stays in the JNI global reference table for the life of the process. ART aborts once the table holds 51,200 entries: JNI ERROR (app bug): global reference table overflow (max=51200), and the dump shows ~50,000 java.nio.DirectByteBuffer entries. The ArrayBuffer behind each pinned buffer is never freed either. JSObjectFinalizer keeps a JS object alive while its Java counterpart is alive, and the pinned DirectByteBuffer never dies. Passing the same ArrayBuffer again doesn't leak, because the existing link is reused. So the leak is one reference per distinct buffer. That matters for any app that hands Java a fresh buffer per event. We hit it in an app that passes each display frame (pixels plus glyph data) to Java as new ArrayBuffers. It aborted after 2.75 to 10.6 hours of use, at 1.4 GB RSS. The fix removes the NewGlobalRef call in both converters. Does your pull request have unit tests? Yes. Two tests in byte-buffer-test.js each pass 60,000 fresh buffers to Java, more than the table can hold: one as method arguments (alternating ArrayBuffer and Uint8Array), one as Java array elements. Run on a Galaxy Z Fold7 (arm64, Android 17) with the runtime built from this branch: Unpatched This branch New tests test app aborts with the overflow (50,398 DirectByteBuffer global refs) pass, ~0.5 s each Full test-app suite 1242 tests, 0 failures (without the new tests) 1244 tests, 0 failures Also checked in the affected app, by heap-dumping it and counting JNI global roots: Fresh buffers: after passing 1,000 fresh buffers to Java, there are no new JNI-global DirectByteBuffers. Unpatched, there were +1,000, and they survived a forced gc(). Collection: after JS and Java GC cycles, the 1,000 buffers are collected. Unpatched, they never were. Contents: 60,000 fresh buffers with known contents hash correctly on the Java side. Kept buffers: a buffer kept alive in JS still hashes correctly after the GC cycles. 🤖 Generated with Claude Code Summary by CodeRabbit Bug Fixes Improved handling of large numbers of JavaScript buffers passed to Java, preventing them from exhausting the runtime’s reference limit. Buffers remain accessible to Java while their JavaScript objects are still in use.
Open Graph Description: Description When a JS ArrayBuffer, SharedArrayBuffer or typed array crosses to Java for the first time, JsArgConverter::ConvertArg wraps it in a direct ByteBuffer, then calls env.NewGlobalRef(buffe...
X Description: Description When a JS ArrayBuffer, SharedArrayBuffer or typed array crosses to Java for the first time, JsArgConverter::ConvertArg wraps it in a direct ByteBuffer, then calls env.NewGlobalRef(buffe...
Opengraph URL: https://github.com/NativeScript/android/pull/2062
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:a2c4c9f9-3334-5bf4-938c-f733f8ebbd50 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D4BA:2F4BD9:17885A9:1F078A1:6B0135D4 |
| html-safe-nonce | bb8810c99c3c5fd4486861ce7ff3fd5d0eaf4ef807e81ac959371a738b539526 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEJBOjJGNEJEOToxNzg4NUE5OjFGMDc4QTE6NkIwMTM1RDQiLCJ2aXNpdG9yX2lkIjoiNjE0MTQ1NzYzNTIxNDEwNTE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | cced90e3c1a36bbf9f39fa8e8614a2ae5fcc8d4afec26bcfba98793f82610b8e |
| hovercard-subject-tag | pull_request:4737667260 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/android/pull/2062/files |
| twitter:image | https://avatars.githubusercontent.com/u/3069027?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/3069027?s=400&v=4 |
| og:image:alt | Description When a JS ArrayBuffer, SharedArrayBuffer or typed array crosses to Java for the first time, JsArgConverter::ConvertArg wraps it in a direct ByteBuffer, then calls env.NewGlobalRef(buffe... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/android git https://github.com/NativeScript/android.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 29137950 |
| octolytics-dimension-repository_nwo | NativeScript/android |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 29137950 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/android |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width