user11081925
user11081925

Reputation:

Why does Node.js Garbage Collector not collect "complied code" of new Function()?

When I run bluebird's promisify() in setInterval(), I found a memory leak problem. There is the issue https://github.com/petkaantonov/bluebird/issues/1663. I doubt it's because the "compiled code" of new Function() can't be collected by gc of nodejs. So I run the following test and record heap snapshot by Chrome DevTools. As time increases, there are more and more "compiled code" of new Function().

setInterval(() => {
    const a = new Function('a', 'console.log(a)');
    a('1');
}, 10);

I want to know why Node.js gc doesn't collect "complied code" of new Function() and whether it is a a bug?

The heap snapshot after running the test script. The heap snapshot

Upvotes: 1

Views: 926

Answers (1)

jmrk
jmrk

Reputation: 40501

Generally, the garbage collector can and does collect compiled code, just like everything else.

Keep in mind that garbage collected systems don't free memory immediately when an object goes out of scope. At some point, the garbage collector will run again, identify unreachable objects, and free their memory.

In this particular case, it looks like DevTools are keeping extra data around (presumably for debugging purposes), which does make this look like a memory leak -- but only while DevTools are open. I've filed crbug.com/1141613 so the team can take a look.

Upvotes: 1

Related Questions