简体   繁体   English

Node.js全局变量属性已清除

[英]Node.js global variable property is purged

my problem is not about "memory leakage", but about "memory purge" of node.js (expressjs) app. 我的问题不是关于“内存泄漏”,而是关于node.js(expressjs)应用程序的“内存清除”。

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). 在启动应用程序后暂时(一两天),一切似乎都很好,直到突然我的Web客户端由于已清除(未定义)对象而无法查找该对象。 I suspect Javascript GC (garbage collection). 我怀疑Javascript GC(垃圾回收)。 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. 但是,正如您在psedu代码中看到的那样,我将对象分配给了node.js的“全局”变量属性,以防止GC清除它们。 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. 我的node.js环境是node.js 0.6.12,expressjs 2.5.8和VMWare cloudfoundry节点托管。

Here is my app.js pseudo-code : 这是我的app.js伪代码:

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. 如果它不在高速缓存中,则从持久性存储(数据库,文件)中获取它,然后对其进行缓存并继续。

Exactly like Haesung I wanted to keep my program simple, without database . 就像Haesung一样,我想让我的程序保持简单,没有数据库 And like Haesung my first experience with Node.js (and express) was to observe this weird purging. 像Haesung一样,我对Node.js(和Express)的首次体验是观察这种奇怪的清除。 Although I was confused, I really didn't accept that I needed a storage solution to manage a json file with a couple of hundred lines. 尽管我很困惑,但我真的不接受我需要一种存储解决方案来管理数百行的json文件。 The light bulb moment for me was when I read this 对我来说,灯泡的时刻是我读到这篇文章时

If you want to have a module execute code multiple times, then export a function, and call that function. 如果要让一个模块多次执行代码,请导出一个函数,然后调用该函数。

which is taken from http://nodejs.org/api/modules.html#modules_caching . 摘自http://nodejs.org/api/modules.html#modules_caching So my code inside the required file changed from this 所以我所需文件中的代码已更改为

var foo = [{"some":"stuff"}];
export.foo;

to that 那个

export.foo = function (bar) {
var foo = [{"some":"stuff"}];
return foo.bar;
}

And then it worked fine :-) 然后工作正常:-)

Then I suggest to use file system, I think 4KB overhead is not a big deal for your goals and hardware. 然后,我建议使用文件系统,我认为4KB的开销对于您的目标和硬件而言并不重要。 If you familiar with front-end javascript, this could be helpful https://github.com/coolaj86/node-localStorage 如果您熟悉前端javascript,这可能会有所帮助https://github.com/coolaj86/node-localStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM