清除Node.js全局变量属性 [英] Node.js global variable property is purged

查看:283
本文介绍了清除Node.js全局变量属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题不是关于内存泄漏,而是关于node.js(expressjs)应用程序的内存清除。

我的应用程序应该保留一些对象内存用于服务期间的快速查找。在启动应用后暂时(一两天),everthing看起来很好,直到突然我的Web客户端无法查找对象,因为它已被清除(未定义)。我怀疑Javascript GC(垃圾收集)。但是,正如您在psedu-code中所看到的,我将对象分配给node.jsglobal变量属性以防止GC清除它们。请给我一些线索是什么导致了这个问题。



我非常感谢您的友善建议〜



我的node.js环境是node.js 0.6.12,expressjs 2.5.8和VMWare cloudfoundry节点托管。



这是我的app.js伪代码: p>

  var express = require(express); 
var app = module.exports = express.createServer();

// myMethods包含一组用于处理原始数据的方法。
var myMethods = require(myMethods);

//创建node.js全局属性引用对象以防止GC清除它们
global.myMethods = myMethods();
global.myObjects = {};

//省略快速配置

//通过id为用户在global.myObjects中创建对象(data1,data2)。
app.post(/ createData /:id,function(req,res){

//为用户创建一个空对象
var myObject = global。 myObjects [req.prams.id] = {};

//获取json数据
var data1 = JSON.parse(req.body.data1);
var data2 = JSON.parse(req.body.data2);

// buildData1& buildData2函数将data1& data2转换为可用对象
//这些函数返回对
myObject.data1 = global.myMethods.buildData1(data1);
myObject.data2 = global.myMethods.buildData2(data2);

res.send(创建新数据,200);
res.redirect(/);
});

//返回用户的data1。
//这里发生问题:运行服务一两天后,myObject变成undefined。
app.get(/ getData1 /:id,function(req,res){

var myObject = global.myObjects [req.params.id];
if (myObject!== undefined){
res.json(myObject.data1);
} else {
res.send(500);
}
}) ;

//省略其他服务回调函数。

// VMWare cloudfoundry node.js托管。
app.listen(process.env.VCAP_APP_PORT || 3000);


解决方案

任何一种缓存系统已知或第三方产品)应考虑到这种情况。您不应该依赖内存缓存中始终可用的数据。有太多的东西可能导致内存中的数据消失(机器重启,进程重启等)。

就你而言,你可能需要更新您的代码以查看数据是否在缓存中。如果它不在缓存中,则从持久性存储(数据库,文件)中获取缓存,然后继续。

my problem is not about "memory leakage", but about "memory purge" of node.js (expressjs) app.

My app should maintain some objects in memory for the fast look-up's during the service. For the time being (one or two days) after starting the app, everthing seemed fine, until suddenly my web client failed to look-up the object bacause it has been purged (undefined). I suspect Javascript GC (garbage collection). However, as you can see in the psedu-code, I assigned the objects to the node.js "global" variable properties to prevent GC from purging them. Please give me some clue what caused this problem.

Thanks much in advance for your kind advices~

My node.js environments are node.js 0.6.12, expressjs 2.5.8, and VMWare cloudfoundry node hosting.

Here is my app.js pseudo-code :

var express = require("express");
var app = module.exports = express.createServer();

// myMethods holds a set of methods to be used for handling raw data.
var myMethods = require("myMethods");

// creates node.js global properties referencing objects to prevent GC from purging them
global.myMethods = myMethods();
global.myObjects = {};

// omited the express configurations

// creates objects (data1, data2) inside the global.myObjects for the user by id.
app.post("/createData/:id", function(req, res) {

    // creates an empty object for the user.
    var myObject = global.myObjects[req.prams.id] = {};

    // gets json data.
    var data1 = JSON.parse(req.body.data1);
    var data2 = JSON.parse(req.body.data2);

    // buildData1 & buildData2 functions transform data1 & data2 into the usable objects.
    // these functions return the references to the transformed objects.
    myObject.data1 = global.myMethods.buildData1(data1);
    myObject.data2 = global.myMethods.buildData2(data2);

    res.send("Created new data", 200);
    res.redirect("/");
});

// returns the data1 of the user.
// Problem occurs here : myObject becomes "undefined" after one or two days running the service.
app.get("/getData1/:id", function(req, res) {

    var myObject = global.myObjects[req.params.id];
    if (myObject !== undefined) {
        res.json(myObject.data1);
    } else {
        res.send(500); 
    }
});

// omited other service callback functions.

// VMWare cloudfoundry node.js hosting.
app.listen(process.env.VCAP_APP_PORT || 3000);

解决方案

Any kind of cache system (whether is roll-your-own or a third party product) should account for this scenario. You should not rely on the data always being available on an in-memory cache. There are way too many things that can cause in-memory data to be gone (machine restart, process restart, et cetera.)

In your case, you might need to update your code to see if the data is in cache. If it is not in cache then fetch it from a persistent storage (a database, a file), cache it, and continue.

这篇关于清除Node.js全局变量属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆