Last active
February 22, 2016 13:17
-
-
Save ggd543/4eaea41b32c497f23e82 to your computer and use it in GitHub Desktop.
关于nodejs vm.runInNewContext()会出现内存泄露的一点说明
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var vm = require('vm'); | |
var exp = require('express'); | |
var app = exp(); | |
var script = new vm.Script(fs.readFileSync("t2.js", 'utf8')); | |
var context = { module: {} }; | |
var fn = script.runInNewContext(context); | |
app.get('/', function(req, res) { | |
fn(); | |
global.gc(); | |
console.log('memory usage ' + process.memoryUsage()); | |
res.send("Hi"); | |
}); | |
app.listen(4000, function() { | |
console.log("Running " + this.address().address + " port " + this.address().port); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.export = function () { | |
var o = {}; | |
function makeid() | |
{ | |
var text = ""; | |
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
for( var i=0; i < 100; i++ ){ | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
} | |
return text; | |
} | |
for(i=0; i<1000; i++) | |
{ | |
o["p_" +i] = makeid(); | |
} | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[这里](https://github.com/nodejs/node-v0.x-archive/issues/9202) 有个issue, 说vm.runInNewContext会导致内存泄露。issue中给出的测试用例是先创建 | |
一个script对象,然后不断调用script.runInNewContext(); 如果先将通过module.exports导出函数,然后再在调用导出的函数, 则不会有内存泄露问题 | |
下面是我的测试用例 | |
运行命令 | |
``` | |
node --expose-gc --trace-gc --max_old_space_size=150 index2.js | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment