AWS Lambda-Nodejs:分配失败-JavaScript堆内存不足
我使用Lambda(nodeJS)查询noSQL数据(dynamodb) 假设我在DynamoDB中有表“student”,我有一个API,它返回特定类(class_id)的学生列表。(我用了“查询”) 据我所知,dynamo对结果进行分页,因此我的API的工作原理如下:AWS Lambda-Nodejs:分配失败-JavaScript堆内存不足,javascript,node.js,aws-lambda,amazon-dynamodb,Javascript,Node.js,Aws Lambda,Amazon Dynamodb,我使用Lambda(nodeJS)查询noSQL数据(dynamodb) 假设我在DynamoDB中有表“student”,我有一个API,它返回特定类(class_id)的学生列表。(我用了“查询”) 据我所知,dynamo对结果进行分页,因此我的API的工作原理如下: {class_id:'xxxx'}=>返回第一个学生列表 {class\u id:'xxxx',上次评估的\u键:{….}=>返回下一个学生列表(如果上次评估的键存在) 我的lambda代码: exports.handle
=>返回第一个学生列表{class_id:'xxxx'}
=>返回下一个学生列表(如果{class\u id:'xxxx',上次评估的\u键:{….}
存在)上次评估的键
exports.handler = function(e, ctx, callback) {
var rp = require('request-promise');
var students = [];
var hasMore = true;
var params = {
class_id: e.class_id
}
while (hasMore) {
var options = {
method: 'POST',
uri: 'https://xxxxxx.eu-west-1.amazonaws.com/dynamodliblightdm-gobject-1-0:amd64liblightdm-gobject-1-0:amd64b/getStudents',
body: params,
json: true // Automatically stringifies the body to JSON
};
rp(options)
.then(function(repos) {
console.log('count: ' + repos.Count);
students.push(repos.Items);
if (repos.hasOwnProperty("LastEvaluatedKey")) {
params['last_evaluated_key'] = repos.LastEvaluatedKey;
} else {
hasMore = false;
}
})
.catch(function(err) {
console.log('Error', err);
});
}
callback(null, 'done.');
}
我得到一个错误:
42676毫秒:标记扫描804.1(954.3)->802.7(954.3)MB,1803.0/0.0
标记开始后的32个步骤中的毫秒(+246.3毫秒,最大步骤35.7毫秒
ms)[分配失败][请求旧空间中的GC]。
44415毫秒:标记扫描802.7(954.3)->802.7(954.3)MB,1738.6/0.0毫秒[分配失败][请求旧空间中的GC]。
46318毫秒:标记扫描802.7(954.3)->809.5(859.3)MB,1902.9/0.0毫秒[最后手段gc]。
48184毫秒:标记扫描809.5(859.3)->816.4(858.3)MB,1865.7/0.0毫秒[最后的gc]。
==JS堆栈跟踪=========================================
安全上下文:0x322e8723fa99
2:新构造函数(也称为Multipart)[/var/task/lambda func/node_modules/request/lib/Multipart.js:~8]
[pc=0x1b47df3f5f98](此=0x1175e583149,请求=0x1175e582fa9)
4:新构造函数(aka Request)[/var/task/lambda func/node_modules/Request/Request.js:125]
[pc=0x1B47DF4DF3DE6](此=0x1175e。。。
致命错误:调用和重试上次分配失败-JavaScript堆内存不足
1:node::Abort()[/var/lang/bin/node]
2:0x55d79ff0b302[/var/lang/bin/node]
3:v8::Utils::ReportApiFailure(char-const*,char-const*)[/var/lang/bin/node]
4:v8::internal::v8::FatalProcessOutOfMemory(char const*,bool)[/var/lang/bin/node]
5:v8::internal::Factory::NewFillerObject(int、bool、v8::internal::AllocationSpace)[/var/lang/bin/node]
6:v8::internal::Runtime_AllocateInTargetSpace(int,v8::internal::Object**,v8::internal::Isolate*)[/var/lang/bin/node]
7:0x1b47df2062bf
欢迎提出任何建议。请向所有学生提出建议
const fetch = (lastEvaluatedKey) => {
return rp().then((res) => {
students = students.concat(res.moreStudents);
if(res.shouldKeepFetching) {
return fetch(res.lastKey);
}
return Promise.resolve();
})
}
fetch().then(() => {
//you fetched them all
})
如果有一大堆学生,这可能会让你出现另一个内存不足的问题。
你可以做的另一件事,但我不知道lambda是否允许,就是使用一个带有async/await的for循环,它看起来像是在无限循环中。while循环并没有等到你的承诺得到解决。它只是一遍又一遍地调用相同的东西。我可以看到问题来自while循环,我的问题是如何使用'request'-while循环中的promise,或者更大的问题是如何正确地从dynamodb获取所有项目。