Report native memory to the GC and add dispose function - #23
Merged
Merged
Conversation
andresovela
requested changes
Sep 3, 2026
andresovela
requested changes
Sep 7, 2026
| /// exactly once: by `dispose()`, or by the finalizer of the last surviving handle. | ||
| /// `Option::take` makes the give-back idempotent, so the two never double-count. | ||
| pub(crate) struct DisposableSlot<T> { | ||
| inner: Mutex<Option<T>>, |
Collaborator
There was a problem hiding this comment.
This doesn't make a lot of sense to me. I think this should just be Option<T>. Currently only the async APIs use a DisposableSlot<T>, but it feels like all API should use them. If anything, async APIs should own a Shared<DisposableSlot<T>> (still not happy with the Shared type, but let's leave that for later).
Contributor
Author
There was a problem hiding this comment.
Yeah that makes sense, I got rid of the Mutex in Disposable Slot and just added it outside where it makes sense.
mgeier
reviewed
Sep 7, 2026
andresovela
approved these changes
Sep 7, 2026
Collaborator
There was a problem hiding this comment.
LGTM. The comments need work but I think that's a broader issue, also in #22. We can merge this into next IMO.
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.
Problem
Model,Processor,ProcessorAsync,Vad,VadAsyncandAnalyzerhold large nativeallocations behind small JavaScript objects. V8's GC never saw that cost:
heapUsedandexternalbarely moved, so the collector felt no pressure to reclaim dropped instances, and aworkload creating processors per unit of work ratcheted RSS up until the process was OOM-killed.
Changes
napi_adjust_external_memory(new
src/mem.rs): the footprint is reported at construction and returned exactly once whenthe object is destroyed, so GC pressure now reflects reality.
dispose()on all six classes for deterministic cleanup without waiting for GC. Afterdispose, every method throws; repeat calls do nothing. Where work can be in flight it blocks
until that work finishes.
Model.dispose()unmaps the model file while objects created fromit keep working;
ProcessorContext/VadContexthandles stay valid but no longer reach alive object.
turns, RSS reflects the peak of live instances).
__test__/dispose.spec.ts(9 tests): dispose semantics, idempotency, model referencecounting, and the dispose-vs-in-flight-
analyzeAsyncraces.