Title: feat(runtime): find the JS/Java cycles canary mode keeps alive (experimental) by edusperoni · Pull Request #2130 · NativeScript/android · GitHub
Open Graph Title: feat(runtime): find the JS/Java cycles canary mode keeps alive (experimental) by edusperoni · Pull Request #2130 · NativeScript/android
X Title: feat(runtime): find the JS/Java cycles canary mode keeps alive (experimental) by edusperoni · Pull Request #2130 · NativeScript/android
Description: Description Fifth of the stacked wrapper-lifetime series (on #2128), and experimental: an Android counterpart of NativeScript/ios#576's leak probe. Canary mode keeps the JS ↔ Java cycles resurrection used to break; this finds them. It is a new design rather than a port: iOS counts outside owners with -retainCount and walks ObjC ivars, and Java has neither retain counts nor a safe in-process field walk with a known root set. What Java does have is an exact heap dump. ns:runtime findLeakCycles() Each heap has its own collector, so neither sees a cycle that crosses into the other. The probe joins both heaps into one graph and asks what a single collector would keep: JS heap: a V8 heap snapshot, taken before the probe holds any handle to a wrapper or state, after a full V8 collection and a settle, so wrappers that died since the last collection no longer read as holding their Java objects. Its roots exclude the runtime's own handles to canary records and resurrected wrappers (the (Global handles) root's edges to those targets), even for records of objects the probe's own Java collection let go of; weak and shortcut edges are skipped. Java heap: an HPROF dump from android.os.Debug.dumpHprofData, parsed natively from a memory map (streaming, two passes: records, then fields by class layout). Its GC roots are used as is, except that the runtime's object table (ObjectTable$Leaf.values), Reference.referent, FinalizerReference.zombie and the probe's own pin array are not followed. The objects the runtime knows are pinned in order in LeakProbe.pinned so the analysis can find them in the dump. The runtime's cross-heap edges: a live wrapper holds its Java object (canaries always, resurrecting wrappers until they are weakened), a Java object holds its canary record (its state and, for a JS-extended object, the first canary's prototype, internal fields and implementation object, plus the shell copied from its last rebuilt canary while it exists; all listed among the record's values, so none reads as a root), and a Java object held weakly for a resurrected wrapper holds that wrapper. A WeakMap value is followed from its key only (V8 also links it from the map's table, which would hide every cycle through a WeakMap value). Java is collected twice before the dump, so objects only weak entries hold are gone. A Java object the runtime holds for a live wrapper, and that nothing else reaches, is leaked: a unified collector would free it. For each, the report gives the shortest path from the Java object to its own wrapper through Java fields, JS properties and the runtime's edges, closed by the wrapper → Java edge; a signature (the path's node labels from their smallest rotation, with V8's @id suffixes stripped); and the leaked classes on it (members). leakedOutsideCycles counts leaked objects on no reported cycle: held by one, or past the search limits (truncated: after 256 searches, or a search that visited 2,000,000 nodes). In "resurrecting" the report is empty and no dump is taken: resurrection breaks these cycles. Report fields: mode, cycles, leaked, leakedOutsideCycles, truncated, probed, jsNodes, javaObjects, dumpBytes, durationMs. Cost Slow by design and on demand only: the dump suspends the process, is written to the app's cache directory (deleted once parsed; a dump a killed process left is swept by the next probe), and both heaps are graphed in native memory. On the test app (arm64 emulator): a 44 MB dump, about 3 s per call. Debug builds only, like iOS's debug-only framework: LeakProbe.cpp and the accessor it reads compile only into debug runtimes (APPLICATION_IN_DEBUG, the optimized-with-inspector AAR). The Java side (LeakProbe.java) lives in a src/debugTools/java source set every runtime but optimized builds, so the release AAR carries none of it, Java or native, but keeps findLeakCycles in ns:runtime, which throws "available in debug builds only". Unlike iOS there is no way to ship it in a release build. Probes in several runtimes run one at a time. The V8 object ids the snapshot assigns are cleared afterwards unless DevTools is connected (a snapshot otherwise leaves V8 tracking object moves on every scavenge). What it found Run on the test app under canary/all, after the suite's own specs had run, it reported, besides the shapes the new spec plants: MyButton9 (a TypeScript Button subclass from testsForTypescript.js) ↔ its View.OnClickListener implementation, whose onClick closure captures the button: the listener cycle canary mode keeps (two instances); several Runnable/Predicate implementations whose run/test closure captures the implementation itself. Example path for the planted list cycle: com.tns.gen.java.lang.Runnable --ns:kept--> JS Object --probeList--> JS java.util.ArrayList --ns:wrapper--> java.util.ArrayList --java.util.ArrayList.elementData--> java.lang.Object[] --[]--> com.tns.gen.java.lang.Runnable. Blind spots JNI global references the app or a plugin holds are GC roots, so cycles through them read as legitimately held, not leaked (missed, not misreported). The V8 snapshot and the Java dump are taken one after the other, not atomically; a reference that moves between them can hide or invent a path. Only the calling runtime's wrappers are modeled: an object held only through a worker's wrappers can read as leaked. Runtime handles are excluded by target, so a record value a real root also reaches counts as reached (correctly), and one reached only through the runtime's handles does not. Testing New testWrapperLifetimeLeaks.js (one probe for the whole suite, on a later task than the planting, since a WeakRef keeps its target alive until its job ends): an object whose JS state references it (two, with equal signatures), a cycle through a Java ArrayList, an object held by a Java static (not reported), a back reference through a WeakRef (not reported), a WeakMap value referencing its key (reported through WeakMap value), a Runnable whose run closes over the object itself (probeClosed: the cycle runs through the record's prototype handle, its implementation object, reported with ns:kept and ns:wrapper edges), a cycle through a Java list whose canary was rebuilt before the probe (probeRebuiltList: a shell read as a root would hide it), and the report's fields; in "resurrecting" no cycle is reported. Each cycle's signature is logged. Release packaging: in the optimized AAR's libNativeScript.so none of the probe's strings are present and the stub's message is; the optimized-with-inspector AAR has the probe. An independent review pass (second commit) fixed: V8 left tracking object moves after the snapshot; cycles through WeakMap values hidden by the table's edge; GC-timing-dependent counts; no gating (now debug builds only); concurrent probes sharing the pin array; no early return outside canary mode; uncapped cycle searches; C++ exceptions not caught; a malformed string record. Full suite on an arm64 emulator, on this branch: 0 failures in each of resurrecting/extended (1839 specs), canary/extended and canary/all (1837 each). The probe itself: about 3 s on a 44 MB dump per run (not re-measured on the current head); at the tip, nothing in "resurrecting" and 16 leaked objects in 15 cycles in canary mode. Summary by CodeRabbit New Features Added a debug-build diagnostic that analyzes JavaScript and Java heap data to identify possible leak cycles, with cycle paths and report metrics. The diagnostic may pause the app while collecting and analyzing a full heap dump. It is unavailable in release builds. Documentation Documented the diagnostic’s availability, report details, limits, and runtime caveats.
Open Graph Description: Description Fifth of the stacked wrapper-lifetime series (on #2128), and experimental: an Android counterpart of NativeScript/ios#576's leak probe. Canary mode keeps the JS ↔ Java cycles resurr...
X Description: Description Fifth of the stacked wrapper-lifetime series (on #2128), and experimental: an Android counterpart of NativeScript/ios#576's leak probe. Canary mode keeps the JS ↔ Java cycles re...
Opengraph URL: https://github.com/NativeScript/android/pull/2130
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:f79a4e25-6959-2089-bb3b-19795607e11a |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | CFD8:280A2E:1AE0DA:231B01:6B0163D5 |
| html-safe-nonce | cb26a05988c13ae1d5732903452ae2c231f2eb3cc548c90f648c2db0bdd1dd18 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkQ4OjI4MEEyRToxQUUwREE6MjMxQjAxOjZCMDE2M0Q1IiwidmlzaXRvcl9pZCI6IjM2ODc5NDgwNTkzMzkyMTk5MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f5586474a60620313442558fa56db112effb387ffc2c3446c1b63f4f14eff416 |
| hovercard-subject-tag | pull_request:5418235401 |
| 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/2130/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | Description Fifth of the stacked wrapper-lifetime series (on #2128), and experimental: an Android counterpart of NativeScript/ios#576's leak probe. Canary mode keeps the JS ↔ Java cycles resurr... |
| 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