Repository navigation
feat(runtime): report native memory held by wrappers to V8, forward memory pressure - #507
Draft
edusperoni wants to merge 1 commit into
Draft
edusperoni wants to merge 1 commit into
edusperoni wants to merge 1 commit into
Conversation
…emory pressure V8 only weighs its own heap (plus ArrayBuffer backing stores) when scheduling collections, so a JS wrapper holding megabytes of native memory looks like a few dozen bytes and can sit uncollected until the process is jetsammed. - Wrappers can carry an ExternalMemoryCharge (a v8::ExternalMemoryAccounter) that is returned whenever the wrapper is deleted, from any thread; once the isolate's gate is closed the decrease is skipped since disposal drops it. - interop.alloc(n) charges n bytes to the returned pointer. - Objects JS likely owns (JS constructors, initializers, +1 returns, class factory methods) get an estimated size: NSData length, UIImage/CGImage bitmap size, CVPixelBuffer data size. CG/CV are resolved at runtime, so no new link dependencies. NSDataAdapter is excluded (V8 already counts it). - interop.setExternalSize(obj, bytes) / interop.getExternalSize(obj) let libraries declare the native footprint of what they wrap. - A process-wide dispatch memory-pressure source forwards normal/warn/critical to every live isolate via Isolate::MemoryPressureNotification.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The runtime never told V8 about native memory kept alive by JS wrappers. V8 only weighs its own heap (plus ArrayBuffer backing stores, which it counts itself) when scheduling GCs. So a wrapper holding a multi-MB
UIImagelooks like ~50 bytes and can sit uncollected until iOS jetsams the app. This PR adds reporting at three levels of confidence and forwards OS memory pressure.Accounting primitive
ExternalMemoryCharge(wrappingv8::ExternalMemoryAccounter, the non-deprecated replacement forAdjustAmountOfExternalAllocatedMemory) hangs offBaseDataWrapper. Whichever path deletes the wrapper (finalizer,__releaseNativeCounterpart, teardown, an adapter's-deallocon another thread) returns the charge.Tier 1: exact sizes the runtime allocates
interop.alloc(n)chargesnbytes to the adopted pointer.Tier 2: estimated sizes for objects JS likely owns
NSData→length;UIImage/CGImage→bytesPerRow * height;CVPixelBuffer→CVPixelBufferGetDataSize. They are resolved once per class and cached inCaches.dlsym, so the runtime gains no link dependencies.NSDataAdapteris excluded, since V8 already counts its JS buffer.-CGImagerasterizes them.new X(...)copy/new/Create)view.image) are not charged, because the receiver usually keeps the object alive and the GCs triggered by charging it could not free anything.Tier 3: library-declared sizes
interop.setExternalSize(obj, bytes)replaces the charge on a native object, pointer, reference, struct, block or function reference; 0 clears it.interop.getExternalSize(obj)reads it back.TypeError, because they live as long as the isolate and the charge could never be returned.[0, 2^34], otherwiseRangeError. V8 aborts the process, rather than throwing, on a single adjustment above its sanity limit.@nativescript/coreImageSource, canvas and plugins, which know their real footprint.Memory pressure
DISPATCH_SOURCE_TYPE_MEMORYPRESSUREsource maps normal/warn/critical toMemoryPressureLevel::kNone/kModerate/kCriticaland callsIsolate::MemoryPressureNotificationon every live isolate.Known limitations
NSMutableDatagrowth is not tracked.interop.bufferFromDataon a chargedNSDatacounts the bytes twice: once for the NSData wrapper, once for the ArrayBuffer.interop.setExternalSize/getExternalSizetypings belong in@nativescript/typesand are a follow-up.Tests
TestRunner/app/tests/ExternalMemoryTests.js(12 specs) covers:interop.allocFull suite on a dedicated simulator: 1794 specs, 0 failures. The memory-pressure path has no automated test because the simulator has no reliable way to raise a memory-pressure event.