码迷,mamicode.com
首页 > Web开发 > 详细

nodejs 计算内存使用率

时间:2015-10-17 01:47:18      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

//计算内存使用率
function calcMem(){
	let mem_total = os.totalmem(),
		mem_free = os.freemem(),
		mem_used = mem_total - mem_free,
		mem_ratio = 0;
	mem_total = (mem_total / (1024 * 1024 * 1024)).toFixed(1);
	mem_used = (mem_used / (1024 * 1024 * 1024)).toFixed(1);
	mem_ratio = parseInt(mem_used / mem_total * 100);
	return {
		total: mem_total,
		used: mem_used,
		ratio: mem_ratio
	}	
}

测试worker

‘use strict‘;
const fork = require(‘child_process‘).fork,
	cpus = require(‘os‘).cpus();
//保存被子进程实例数组
var workers = {};
//这里的被子进程理论上可以无限多
var appsPath = [‘./os.js‘,‘./os.js‘,‘./os.js‘];
var createWorker = function(appPath){
    //保存fork返回的进程实例
    var worker = fork(appPath);
    //监听子进程exit事件
    worker.on(‘exit‘,function(){
        console.log(‘worker: ‘ + worker.pid + ‘ exited‘);
        delete workers[worker.pid];
        createWorker(appPath);
    });
    workers[worker.pid] = worker;
    console.log(‘Create worker: ‘ + worker.pid);
}
//启动所有子进程
for (var i = appsPath.length - 1; i >= 0; i--) {
    createWorker(appsPath[i]);
};
//父进程退出时杀死所有子进程
process.on(‘exit‘,function(){
    for(var pid in workers){
        workers[pid].kill();
    }
})

  

nodejs 计算内存使用率

标签:

原文地址:http://www.cnblogs.com/zh33gl/p/4886746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!