-
Notifications
You must be signed in to change notification settings - Fork 460
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
Question: Persistent reference & Garbage Collector #985
Comments
Errmmm... try putting your JS code into something that would pop scope. This is just a guess: you are global.gc()'ing but the object created by the // test.js
const lib = require('bindings')('gctest');
{
let context = new lib.Context();
new lib.XSocket({ context });
}
global.gc(); |
@KevinEady Tried, it has the same behavior. |
You can't depend on when an object will be collected. Once there is no longer a reference it's up to the gc to decide when/if it wants to free the object in order to reclaim space. We do use things like global.gc() in tests because we want to be able to test out scenarios, but those are prone to break/fail as the gc evolves and for non-test code you really want to avoid anything which depends on the timing of when the gc will free objects (including avoiding the use of finalizers whenever possible). Even if you go get it to do what you want now, it will quite likely stop doing want you want at some time later. In terms of memory leaks what I generally do it try to validate that it will "never" get freed. If it's just a matter of time then it is "working as expected". If you can show that you will run out of memory (assuming you give the app a reasonable heap) before it gets freed that is one way to identify if it is a leak or just the gc not getting around to freeing the memory. So for example you can run the code that may have a leak in a loop. If the heap size continuously grows until you get an OOM then you may have a leak. If it runs for a long time then most likely not. I often fix the heap at something like 128M to shorten the time before an OOM will occur (however, I'd avoid going too small as that often then just results in there not being enough memory for the app to run at all).. |
There has not been an further discussion for a month. I'm going to close this. Please let us know if you think that was not the right thing to do. |
Before I explain, please do keep in mind that I recently started exploring the V8 & NAPI.
So the question, I have two objects (ObjectWrap) and one object depends on another one (passed via the argument). Because of this, I'm creating a Persistent reference to it.
So my problem is that whenever I'm releasing the reference to it (
ref.Reset()
) the object that was referenced gets garbage collected ~10 seconds later.Is there any way to speed it up (i.e., collect it immediately), or am I doing something wrong? If so, what is the right way to make an object depend on another object?
Here's the PoC:
output:
Thank you.
The text was updated successfully, but these errors were encountered: