简体   繁体   中英

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 aa bug?

The heap snapshot after running the test script. 堆快照

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM