当前位置: 首页 > 知识库问答 >
问题:

AWS lambda-nodejs:分配失败-JavaScript堆内存不足

敖淮晨
2023-03-14

我正在使用Lambda(nodeJS)查询noSQL数据(dynamo db)。

    null
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.0ms(标记开始后32步+246.3ms,最大步35.7ms)[分配失败][请求旧空间中的GC]。44415 ms:标记-扫描802.7(954.3)->802.7(954.3)MB,1738.6/0.0ms[分配失败][请求旧空间中的GC]。46318毫秒:标记-扫描802.7(954.3)->809.5(859.3)MB,1902.9/0.0ms[最后手段气相色谱]。48184 ms:标记-扫描809.5(859.3)->816.4(858.3)MB,1865.7/0.0ms[最后手段气相色谱]。<---JS stacktrace-->===================================安全上下文:0x322E8723FA99 2:新构造函数(又名多部分)[/var/task/lambda-func/node_modules/Request/lib/Multipart.JS:~8][pc=0x1b47df3f5f98](this=0x1175e583149,Request=0x1175e582fa9)4:新构造函数(又名请求)[/var/task/lambda-func/no de_modules/Request/Request.JS:125][pc=0x1b47df4df3e6](this=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: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

任何建议都很感激。

共有1个答案

贝研
2023-03-14

建议让所有的学生。

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
})

如果有一堆学生,这可能会让你有另一个内存不足的问题。您可以做的另一件事是使用带有Async/await的for循环,但我不知道lambda是否允许

 类似资料: