1

监控nodejs的土方法

console.log("Server PID", process.pid)
sudo node --inspect app.js

while true; do curl "http://localhost:1337/"; done

cb -n 1000 -c 100 http://127:0:0:1:8999/
top -pid 2322

几种nodejs内存泄漏原因

无限制增长的数组

无限制设置属性和值

任何模块内的私有变量和方法均是永驻内存的

大循环,无GC机会

function LeakingClass() {
    
}

var leaks = []

var http = require('http')

http.createServer((req, res) => {
    leaks.push(new LeakingClass());
    res.writeHead(200);
    res.end('Hello world');
}).listen(9000)

console.log('Server Pid', process.pid)
_.memoize = function(func, hasher) {
    var memo = {}
    hasher || (hasher = _.identity)
    return function() {
        var key = hasher.apply(this, arguments);
        return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
    }
}
((exports, require, module, __filename, __dirname) => {
    var circle = require('./circle.js')
    console.log('The area of a ' + circle.area(4))
    exports.get = () => circle()
})


for (var i = 0; i < 10000000; i++) {
    var user = {}
    user.name = 'outman'
}

for (var i = 0; i < 1000000; i ++) {
    setTimeout(() => {
        var user = {}
        user.name = 'outman'
    }, 1)
}
var http = require('http');
var crypto = require('crypto');
http.createServer((req, res) => {
    var password = 'ear';
    var salt = crypto.randomBytes(128).toString('base64');
    var hash = crypto.pbkdf2Sync(password, salt, 10000, 512);
    res.writeHead(200);
    res.end('wrwer')
}).listen(1337)

console.log('Server Pid', process.pid)
var http = require('http');
var crypto = require('crypto');
http.createServer((req, res) => {
    var password = 'ear';
    var salt = crypto.randomBytes(128).toString('base64');
    crypto.pbkdf2(password, salt, 10000, 512, () => {
        res.writeHead(200);
        res.end('wrwer')
    })
}).listen(1327)

console.log('Server Pid', process.pid)

林小新
371 声望11 粉丝

想写非常炫酷的3D效果