Freeing a large node module from memory

I am loading a large js module into memory and I want to free it when I no longer need to free RAM.

The code I use for testing is as follows:

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

After 5 seconds, the process still uses the same memory as after the initial loading of the module.

What am I doing wrong? Thanks!

+4
source share
1 answer

Delete Require cache will help you to load contents from the cache again, since it will not delete or free your memory

+1
source

Source: https://habr.com/ru/post/1623534/


All Articles