[英]NodeJS: Reached heap limit Allocation failed - JavaScript heap out of memory
[英]AWS Lambda - Nodejs: Allocation failed - JavaScript heap out of memory
我正在使用Lambda(nodeJS)查詢noSQL數據(dynamo db)。
假設我在DynamoDB中具有表“ student”,並且我有一個API,該API返回特定班級(class_id)的學生列表。 (我使用“查詢”)
據我所知,發電機對結果進行分頁,因此我的API的工作方式如下:
{class_id : 'xxxx'}
=>返回第一名學生名單 {class_id : 'xxxx', last_evaluated_key: { ....}
=>返回學生的下一個列表(如果存在LastEvaluatedKey
) 我的lambda代碼:
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 ms:打標掃描804.1(954.3)-> 802.7(954.3)MB,1803.0 / 0.0 ms(自打標開始,分32步,+ 246.3 ms,最大步長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堆棧跟蹤---> ==== JS堆棧跟蹤================================= ========安全上下文:0x322e8723fa99 2:新的構造函數(又名Multipart)[/var/task/lambda-func/node_modules/request/lib/multipart.js:~8] [pc = 0x1b47df3f5f98](this = 0x1175e583149,請求= 0x1175e582fa9)4:新的構造函數(aka請求)[/var/task/lambda-func/node_modules/request/request.js:125] [pc = 0x1b47df4df3e6](此= 0x1175e ...致命錯誤: CALL_AND_RETRY_LAST分配失敗-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循環
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.