Skip to content
Permalink
Browse files

fix:memory-leak when chean require.cache

  • Loading branch information...
loviselu
loviselu committed Sep 5, 2018
1 parent 1499e80 commit 6d9db67bba2b8411ee2a699da9b8c7b77a57c552
Showing with 26 additions and 10 deletions.
  1. +26 −10 bin/tsw/util/cache.cleaner.js
@@ -27,18 +27,34 @@ this.clear = function(dir, showLog) {
});

for (key in require.cache) {
require.cache[key].children = [];
require.cache[key].resolveFilenameCache = {};

if (key.indexOf(dir) >= 0 && !/\.node$/i.test(key)) {
delete require.cache[key].parent;
delete require.cache[key];
if (showLog) {
logger.debug('clear: ${key}', {
key: key.slice(dir.length)
});

if (key.indexOf(dir) >= 0) {

let mod = require.cache[key];

if(!/\.node$/i.test(key)){

let parentChildren = mod.parent && mod.parent.children;
if(parentChildren){
let idx = parentChildren.indexOf(mod);
if (idx >= 0){
parentChildren.splice(idx, 1)
}
}
delete require.cache[key];

if (showLog) {
logger.debug('clear: ${key}', {
key: key.slice(dir.length)
});
}

}else{
mod.resolveFilenameCache = {};
require.cache[key].parent = null;
}
}
}

};

0 comments on commit 6d9db67

Please sign in to comment.
You can’t perform that action at this time.