gpt4 book ai didi

node.js - 如何追踪 Node.js 和 Express 内存泄漏?

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:18 24 4
gpt4 key购买 nike

我们正在使用看似简单的 Node.js 和 Express 服务器为我们的 Angular.js 项目提供服务,如下所示。它背后的想法是,如果客户端请求的服务器上存在一个文件,比如 main.css,那么它将由 express 静态中间件返回。否则我们假设它是像/account 这样的角度路由,我们返回 index.html 文件以便 Angular 可以接管。

在附件中,您可以看到 Heroku 上的内存激增,然后 Heroku 正在终止服务器并重新启动它。然后它再次出现尖峰...

Memory pattern

这是服务器:

    var newrelic = require('newrelic')
, fs = require('fs')
, express = require('express')
, app = express()
, ENV = process.env.NODE_ENV || 'development'
, ENV_PROD = ENV == 'production'
, port = process.env.PORT || 8080;

console.log('ENV: ' + ENV);

// force ssl
app.use(function(req, res, next){
if (ENV_PROD) {
var protocol = req.get('x-forwarded-proto');
if (protocol != 'https') {
res.redirect(301, 'https://' + req.get('host') + req.url);
} else {
res.header('Strict-Transport-Security', 'max-age=31536000');
next();
}
} else {
next();
}
});

// set default headers
app.use(function(req, res, next){
var cache = ENV_PROD ? 3600 : 0;
res.set('Cache-Control', 'public, max-age='+cache);
res.set('Vary', 'Accept-Encoding');
next();
});

// static cache headers
app.use('/bower_components*', function(req, res, next){
res.set('Cache-Control', 'public, max-age=2419200');
next();
});

// set static directory
app.use(express.static(process.cwd() + '/build'));

// deeplink
app.use('/dl/*', function(req, res, next){
var path = req.baseUrl.replace('/dl/', '');
var url = 'https://www.deeplink.me/tablelist.com/' + path;
return res.redirect(url);
});

// file not found, route to index.html
app.use(function(req, res, next){
res.sendFile(process.cwd() + '/build/index.html');
});

// error handling
app.use(function(err, req, res, next){
res.status(404).send(err.message);
});

app.listen(port, function() {
console.log('Listening on port: ' + port);
});

module.exports = app;

有没有人看到任何可能导致这种情况的突出情况?使用最新版本的 Node 0.10.31 和 Express 4.8.6

更新:2014 年 8 月 29 日

我对 server.js 文件进行了一些更改,删除了 Express 的 sendFile 和静态中间件。您可以在下面看到更新后的图表,它看起来确实更好,但使用的内存比我个人希望的要多。更新的服务器也在下面。

Updated memory usage

    var newrelic = require('newrelic')
, fs = require('fs')
, express = require('express')
, app = express()
, ENV = process.env.NODE_ENV || 'development'
, ENV_PROD = ENV == 'production'
, SSL = ENV_PROD || process.env.SSL
, port = process.env.PORT || 8080
, config = require('../config')[ENV];

console.log('ENV: ' + ENV);

// force ssl
app.use(function(req, res, next){
if (SSL) {
var protocol = req.get('x-forwarded-proto');
if (protocol != 'https') {
res.redirect(301, 'https://' + req.get('host') + req.url);
} else {
res.header('Strict-Transport-Security', 'max-age=31536000');
next();
}
} else {
next();
}
});

// set default headers
app.use(function(req, res, next){
var cache = ENV == 'production' ? 3600 : 0;
res.set('Cache-Control', 'public, max-age='+cache);
res.set('Vary', 'Accept-Encoding');
next();
});

// static cache headers
app.use('/bower_components*', function(req, res, next){
res.set('Cache-Control', 'public, max-age=2419200');
next();
});

// add isFile to req
app.use(function(req, res, next){
var fileName = req.path.split('/').pop();
var isFile = fileName.split('.').length > 1;
req.isFile = isFile;
next();
});

// static
app.use(function(req, res, next){
if (req.isFile) {
var fileName = req.path.split('/').pop();
var path = process.cwd() + '/build' + req.path;
fs.readFile(path, function(err, data){
if (!data) return next();
res.contentType(fileName);
res.send(data);
});
} else {
next();
}
});

// file not found, route to index.html
app.use(function(req, res, next){
if (req.isFile) {
next(new Error('Not found'));
} else {
var path = process.cwd() + '/build/index.html';
fs.readFile(path, function(err, data){
if (err) return next(err);
res.contentType('index.html');
res.send(data);
});
}
});

// error handling
app.use(function(err, req, res, next){
res.status(404).send(err.message);
});

// start server
app.listen(port, function() {
console.log('Listening on port: ' + port);
});

module.exports = app;

最佳答案

这都是因为模块'newrelic'。我们在 Node 上有各种不同编写的应用程序,它们都有问题。无论您使用什么版本的“newrelic”,1.3.2 - 1.11.4,问题总是一样的,内存爆炸了。

只需尝试注释 require('newrelic') 行,您就会发现内存消耗保持下降。

关于node.js - 如何追踪 Node.js 和 Express 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25550527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com