Node在linux上占用内存无限制增长
发布于 1 个月前 作者 hengguoguo 417 次浏览 来自 问答

同样的代码,在windows环境下占用内存增长到60M左右,就在这个范围上下浮动。而在linux下,短短几分钟上升到200多M,而且还在持续增长中。 PS:客户端每3秒reload一下。 后台部分代码如下(集群模式、非集群模式下,效果一样):

var getConfig = require('./getConfig');
var gpath = require('path');
var gupload = require('common/attachmentOper');
var gfs = require('fs');

var StartServer = function () {
    this.express = require('../public_nodejs/express');
    this.http = require('http');
    this.path = gpath;
    this.log4 = require('./Log4logger');
    this.fs = gfs;
    this.upload = gupload;
    this.uploadPath = require('./config').filePath;

    require('./DateFormat');    //以便于在Date、String的原型链上追加格式化时间的函数
    require('./array');         //以便于在Array的原型链上追加函数
    require('./string');         //以便于在String的原型链上追加函数
};

/*
 *服务端启动的方法
 *参数类型为object 提供的属性有:
 *port(必须)  端口号
 *routes(必须) 当前项目的控制器(路由)
 *dirname(必须) 当前项目根目录所在的绝对位置
 *isSession(默认false) 是否启用session
 */
StartServer.prototype.Start = function () {
    var self = this;

    var port, routes, dirname, isSession;
    if (arguments.length > 1) {
        port = arguments[0];
        routes = arguments[1];
        dirname = arguments[2];
        isSession = arguments[4] || false;
    } else {
        var arguObj = arguments[0];
        port = arguObj.port;
        routes = arguObj.routes;
        dirname = arguObj.dirname;
        isSession = arguObj.isSession || false
    }

    dirname = dirname.replace(/\\/g, '/');
    var upth = this.path.resolve(dirname, '../../' + this.uploadPath).replace(/\\/g, '/');
    var lastPth = this.path.basename(dirname);
    this.uploadPath = upth + '/' + lastPth;

    this.upload.uploadPath = this.uploadPath;
    this.upload.projectName = lastPth;
    this.upload.existsDir();
    this.upload.clearFile();

    var app = this.express();
    app.set('port', port);
    app.set('views', dirname + '/views');
    app.engine('.html', require('../public_nodejs/ejs').__express);
    app.set('view engine', 'html');

    //log4js日志
    this.log4.CreateLog(app, dirname);

    //app.use(express.favicon());
    app.use(this.express.logger('dev'));
    app.use(this.express.bodyParser({ uploadDir: this.uploadPath }));
    app.use(this.express.favicon());

    app.use(this.express.methodOverride());
    app.use(this.express.cookieParser());

    //以内存存储session
    if (isSession === true) {
        app.use(this.express.session({
            secret: port
        }));
    }

    app.use(app.router);
    if (routes instanceof Array) {//具有多个路由文件,以数组的形式传入
        for (var i = 0; i < routes.length; i++) {
            routes[i](app);
        }
    } else {//只有一个路由文件
        routes(app);
    }
    app.use(self.express.static(self.path.join(dirname, 'public')));

    //var ser = self.http.createServer(app).listen(app.get('port'), function () {
    //    console.log('Express server listening on port ' + app.get('port'));
    //});

    var cluster = require('cluster');
    var os = require('os');
    // 获取CPU 的数量
    var numCPUs = os.cpus().length;
    if (cluster.isMaster) {
        // 主进程分支
        cluster.on('exit', function (exitWorker, code, signal) {
            var exitCode = exitWorker.process.exitCode;
            console.log('工作进程 ' + exitWorker.process.pid + ' 被结束(' + exitCode + ')。正在重启...');
            cluster.fork();
        });
        // 初始开启与CPU 数量相同的工作进程
        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }
    } else {
        app.listen(app.get('port'));
    }
};

module.exports = new StartServer();
9 回复

session不过期,怎么都是涨

很有可能是 系统 cache了 查看一下系统内存占用情况

@gavin-hao 系统cache?是什么意思,指哪方面?

@hengguoguo @louislve 遍历数组元素或者字符串中的字符时有问题

@flyingcodes 啊,能否详细说下?

回到顶部