Add getUserSharedBuffer() callback finalizer to fix memory leak #323
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.
When
getUserSharedBuffer()
is called with acallback
, the callback is added to an internalstd::unsorted_map
. As far as I can tell, the callback function ref is not GC'd until the env is closed or Node.js exits. We can tie the life of the callback to the life of the returned ArrayBuffer.If we call
getUserSharedBuffer()
in a loop, it will consume memory until Node.js runs out of memory on the heap and crashes. Note that the loop needs to yield in order for GC to collect the unused returned shared buffer.Before this fix, Node.js would crash after 37M iterations. After this fix, Node.js will not crash.
Further more, if we cap the iterations to 4,222,600 (arbitrary, non-magical number), the code above will have consumed 32.2MB of memory, however we can see the V8 function ref is not being released.