node.js – 从内存中释放大节点模块

我正在将一个大的js模块加载到内存中,我想在不再需要释放RAM时释放它.

我用来测试的代码是这样的:

var lex = require('./lex.js'); //Big module (10M array)

setInterval(() => console.log(process.memoryUsage()), 1000);

setTimeout(() => {
    lex = null;
    delete require.cache[require.resolve('./lex.js')];
}, 5000);

// this script outputs each second
// { rss: 151756800, heapTotal: 131487520, heapUsed: 108843840 }
// { rss: 151760896, heapTotal: 131487520, heapUsed: 108850024 }
// ...
// and after 5 seconds there is no change

5秒后,该过程仍然使用与初始模块加载后相同的内存.

我究竟做错了什么?谢谢!

最佳答案 删除需要缓存将帮助您再次加载不是来自缓存的内容,因为我不会删除或释放您的内存

点赞