Description
Version
17.5.0, 16.14.0, does not happen in 14.19.0
Platform
Microsoft Windows NT 10.0.22557.0 x64
Subsystem
vm
What steps will reproduce the bug?
// test.js
const vm = require('vm')
const code = `console.log("${'hello world '.repeat(1e5)}");`
while (true) {
for (let i = 0; i < 30; i++)
vm.compileFunction(code, [], {
// comment out the following line to make it no longer leaking memory
importModuleDynamically: () => {},
})
if (typeof gc !== 'undefined') gc()
console.log(
Math.round(process.memoryUsage().heapUsed / 1024 / 10.24) / 100,
'MiB'
)
}
Run this piece of code with node --expose-gc test.js
.
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior?
Memory usage should stay constant.
You can also comment out the importModuleDynamically
option to see the expected behavior.
Here is what I see:
4.32 MiB
4.34 MiB
4.33 MiB
4.33 MiB
4.33 MiB
4.33 MiB
4.34 MiB
4.34 MiB
4.34 MiB
4.34 MiB
4.35 MiB
What do you see instead?
You will see memory usage increasing in an unexpected way.
Here is what I see:
5.46 MiB
5.54 MiB
5.57 MiB
5.6 MiB
5.62 MiB
5.66 MiB
5.68 MiB
5.71 MiB
5.97 MiB
5.78 MiB
5.81 MiB
5.85 MiB
5.88 MiB
5.91 MiB
5.94 MiB
5.98 MiB
6 MiB
6.03 MiB
6.06 MiB
6.09 MiB
6.12 MiB
6.15 MiB
6.19 MiB
6.22 MiB
6.25 MiB
6.28 MiB
6.31 MiB
6.34 MiB
6.37 MiB
6.4 MiB
6.43 MiB
Additional information
There is more information in this issue: vercel/next.js#34659 (comment)
I think this leak was introduced by this commit: bf2f2b7#diff-c1d48dc599e8281b0be28ab449ea52917f6d4cc0578adb61ae99c631078b1a41R387
This commit could also be related: 89e4b36
Here is the code that causes the leak in my opinion:
Lines 380 to 382 in 45b5ca8
Here is another piece of code that could be relevant:
Line 1197 in 6847fec
And this piece of code:
Lines 1234 to 1248 in 6847fec
And there is this documentation about BaseObject
: https://github.com/nodejs/node/tree/master/src#lifetime-management
BaseObject also defines a MakeWeak
, which is not used in that case, but might be potentially relevant:
Lines 111 to 130 in 470c284