-
Notifications
You must be signed in to change notification settings - Fork 285
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
Memory leak with no stored references #2541
Comments
Simplified code without any external dependency: 'use strict';
const fs = require('fs');
const util = require('util');
const v8 = require('v8');
for(let i = 0; i !== 5e5; i++)
new util.TextDecoder().decode(Buffer.from('abcde'));
setTimeout(() => {
gc();
console.log(process.memoryUsage().heapUsed / (1 << 20), 'MB');
v8.getHeapSnapshot().pipe(createWriteStream('./heap.heapsnapshot'));
}, 1e3); |
This eventually crashes the process: while(1) new util.TextDecoder().decode(Buffer.from('abcde')); Seems like a memory leak in |
@Hakerh400 Thank you for taking a look! I honestly hadn't considered investigating Node's internals for a memory leak. It doesn't look as if anyone has reported this issue or made a fix for it, and it still occurs on Node v13.11.0. I'll open this issue on the main NodeJS repository with this additional information. |
I own the lavalibs/encoding repository and I am encountering a strange memory leak that I cannot resolve. This package consists of two pure functions that de/serialize a specialized encoding, so it does not allocate any state outside of structures needed to perform the de/serialization. One of my users was investigating this problem and wrote a script (below) that I was able to use to verify the memory leak. This script consistently resulted in memory usage over 400MB.
I eliminated the
base64-js
package as a source for the memory leak by running only theBase64.toByteArray
function. Likewise I also verified that usingBuffer.alloc
results in expected memory usage (multiple allocs get GC'd as expected). I also ran the script with--expose-gc
and explicitly triggered GC before generating the heapdump. The heapdump shows a lot of functions and arrays in memory, but I wasn't able to determine any useful information from that.I am confused about how this script is causing a memory leak, since all of the decoded data should be marked for GC immediately after the loop iteration. I'm hypothesizing that the memory leak is related to either the
TextDecoder
(used to read binary data into the decoded structure) orDataView
(used to read specific slices from the inputUint8Array
) potentially keeping some sort of circular reference preventing GC, but I was unable to determine anything more specific than that.I know that investigating memory leaks and heapdumps is not a fun or exciting experience, but I'm really lost on what could be causing this issue. Thank you for any insight you can provide.
Edit: note where we explicitly triggered GC
The text was updated successfully, but these errors were encountered: