You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The garbage collector does not collect objects while executing in a loop #1636
The garbage collector does not collect objects while executing in a loop, whether it is while(true) or for()
This is a minimal example. In fact, I have an eternal while(true) in my project, which calls the synchronous transaction code to the database. However, during the cycle, objects are not deleted at all, which completely eats up the server's memory. There is an option to wrap the code in an asynchronous function and write await at the end of the cycle body (for example, waiting for a promise for 100 ms) and then the collector is triggered, but why doesn't it trigger on global.gc() then?
The default implementation of ObjectWrap defers finalization of objects to a second-pass to allow running JavaScript code during finalization.
You have two options:
Define preprocessor directive NAPI_EXPERIMENTAL and notNODE_API_EXPERIMENTAL_BASIC_ENV_OPT_OUT
Allow the while-loop to break out of the current JavaScript event loop iteration, by making the function async and putting some sorta await ... in the loop.
Thank you, both options work... Although the first option is not very convenient for practical use... And the second one looks too "redundant" and not obvious. Maybe in the future some other method is planned, for example, when implementing your own close() method in c++ code, directly call garbage collection, without --expose-gc?
Thank you for your feedback with the NAPI_EXPERIMENTAL fix. This gives us additional confidence that the implementation works for the users.
Maybe in the future some other method is planned, ...
Eventually, the NAPI_EXPERIMENTAL behavior will not be experimental, and will become the default [edit to add] based on the target Node-API version you target during build. Until then, you must opt-in to the upcoming behavior with the define.
changed the title [-]The garbage collector does not collect objects while executing in a loop[/-][+]WAITING ON experimental feature promotion - The garbage collector does not collect objects while executing in a loop[/+]on Feb 14, 2025
changed the title [-]WAITING ON experimental feature promotion - The garbage collector does not collect objects while executing in a loop[/-][+]The garbage collector does not collect objects while executing in a loop[/+]on Feb 14, 2025
We've recorded that you are waiting on the API to be promoted in a tracking issue for the next Node-API version. Seems like the question has been answered, closing.
Activity
KevinEady commentedon Feb 9, 2025
Hi @Keller18306 ,
The default implementation of ObjectWrap defers finalization of objects to a second-pass to allow running JavaScript code during finalization.
You have two options:
NAPI_EXPERIMENTALand notNODE_API_EXPERIMENTAL_BASIC_ENV_OPT_OUTawait ...in the loop.Please take a look at https://nodejs.org/api/n-api.html#node_api_basic_finalize for more information.
Adding
#define NAPI_EXPERIMENTALto the top of your file, and changing your loop to unseta:Let us know if you have any other questions!
Keller18306 commentedon Feb 9, 2025
Thank you, both options work... Although the first option is not very convenient for practical use... And the second one looks too "redundant" and not obvious. Maybe in the future some other method is planned, for example, when implementing your own close() method in c++ code, directly call garbage collection, without --expose-gc?
KevinEady commentedon Feb 10, 2025
Thank you for your feedback with the
NAPI_EXPERIMENTALfix. This gives us additional confidence that the implementation works for the users.Eventually, the
NAPI_EXPERIMENTALbehavior will not be experimental, and will become the default [edit to add] based on the target Node-API version you target during build. Until then, you must opt-in to the upcoming behavior with the define.[-]The garbage collector does not collect objects while executing in a loop[/-][+]WAITING ON experimental feature promotion - The garbage collector does not collect objects while executing in a loop[/+][-]WAITING ON experimental feature promotion - The garbage collector does not collect objects while executing in a loop[/-][+]The garbage collector does not collect objects while executing in a loop[/+]mhdawson commentedon Feb 14, 2025
We've recorded that you are waiting on the API to be promoted in a tracking issue for the next Node-API version. Seems like the question has been answered, closing.