-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Assertions to track garbage collection (via FinalizationRegistry) #7193
Comments
Interesting, but I'm not sure if there's anything specially needed from Vitest for this 🤔 Users can simply implement such |
well, it turns out, Jest has this: https://github.com/jestjs/jest/tree/main/packages/jest-leak-detector and as you suggested, there might not be anything specially needed from the test framework to support this - though this particular package does have a a lot of dependencies, and I don't understand if this would work outside of Jest.
I found that out after doing some digging - I think this Jest package tries to get around that somehow, but I don't fully understand what it's doing, or even where these is there any reason of course, it's not currently using it, so no reason to enable it - but that might be one reason to have it built-into the test framework? so you get something that "just works" without first having to figure out how to get it working. idk, feel free to close the issue, I guess, if this feels out of scope or too rare to include as a built-in feature. 🙂 |
It looks like Jest uses it for I think we can say this is a niche feature and probably people can explore in user land for now.
I don't think it makes much sense to enable arbitrary flags which aren't enabled in a real code as that can potentially lead to inconsistency between test and production runtime. |
Clear and concise description of the problem
I'm writing a test suite for a library that manages a bidirectional graph - I want to verify that nodes in the graph get correctly detached and become available for garbage collection, so as not to leave behind references that point to unused partitions of the graph... to prevent memory leaks.
Suggested solution
Here's a rough example, which (maybe) proves that it's (very maybe) possible (?) 🤷♂️
This needs to be run with
node --expose-gc
so we can callglobal.gc()
to force garbage collection.Of course, this is just a sketch, suggesting (maybe) it's possible - we might want an API something like:
We might want a
timeout
argument as well, causing the promise to get rejected.I don't know if that's the ideal API - I'm only barely starting to understand the finalization registry.
The
vitest
CLI itself would need to launchnode
with--expose-gc
or it won't work.Alternative
Slow tests that run things thousands of times and checks the memory usage? yeesh idk.
Additional context
This nice article about the finalization registry clued me in:
https://dev.to/codux/experiments-with-the-javascript-garbage-collector-2ae3
Validations
The text was updated successfully, but these errors were encountered: