Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference & SharedReference design issue wrt. memory consumption #1964

Open
lbarthon opened this issue Feb 20, 2024 · 0 comments
Open

Reference & SharedReference design issue wrt. memory consumption #1964

lbarthon opened this issue Feb 20, 2024 · 0 comments

Comments

@lbarthon
Copy link
Contributor

I created a bit earlier this week the #1952 issue, which has been closed, but I'm still seeing a problem.

The "memory leak" naming was incorrect. Per my understanding, what happens is that if a struct B has a SharedReference<A, ()>, the pointer to the data that needs the reference (in that case, ()) is only cleaned up when the finalize_callbacks run for that said instance of A - see code here https://github.com/napi-rs/napi-rs/blob/main/crates/napi/src/bindgen_runtime/js_values/value_ref.rs#L137-L142

This means that if I have an object A that lives for a very long time (potentially even global), and I have many objects B that live for a short period of time, the pointer to what those objects hold will only be cleaned up when A is cleaned up, which might never happen. This will work, and will not be a problem in any use case where A doesn't live for too long (although it'll use more memory).

This reference dropping would need to work bilaterally. If B has a SharedReference on A, when B is dropped it needs to be dropped, and vice-versa. It seems that napi-rs misses half of that logic.

I tried implementing it myself, adding a Drop impl on SharedReference, but without success. Any implementation would also need a way to know if the pointer has been dropped already...or not, which seems like a hassle.

In the meantime, do you see a way for us to use lifetimes more efficiently? I might just declare my global in napi-exposed functions, and go from there. Thanks!

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

No branches or pull requests

1 participant