简体   繁体   English

Node.js Promise.all - JavaScript 堆出 memory

[英]Node.js Promise.all - JavaScript heap out of memory

I tried to fetch some data in node.js in parallel, so the program is faster compared to fetching them sequentially.我尝试并行获取 node.js 中的一些数据,因此与顺序获取它们相比,该程序更快。 I do it like that:我这样做:

function func() {
  var promises = [];
  for (let route of routes) {
    promises.push (calc(route, amount_wei));
  }
  Promise.all(promises).then(function(completedItems) {
    completedItems.forEach(function(val) {
      var result_amount = val;
      if (result_amount[5] > amount_start * 1) {
        console.log("Good Trade");
      }
    }
  });
}

I got help for this code here .我在这里得到了这段代码的帮助。

I do have like 10 routes, so the for loop should run 10 iterations.我确实有 10 条路线,所以 for 循环应该运行 10 次迭代。 But no matter what I try, I got an error:但无论我尝试什么,我都会收到一个错误:

<--- Last few GCs --->

[23272:000001FDFC5C4DE0]   126463 ms: Mark-sweep (reduce) 9852.7 (10047.2) -> 9852.3 (10047.9) MB, 9776.7 / 0.0 ms  (average mu = 0.350, current mu = 0.000) allocation failure scavenge might not succeed
[23272:000001FDFC5C4DE0]   136795 ms: Mark-sweep (reduce) 9853.4 (10047.9) -> 9853.0 (10048.7) MB, 10314.7 / 0.0 ms  (average mu = 0.201, current mu = 0.002) allocation failure scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF6487D7C4F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+114207
 2: 00007FF648765EC6 DSA_meth_get_flags+65542
 3: 00007FF648766D7D node::OnFatalError+301
 4: 00007FF64909B6CE v8::Isolate::ReportExternalAllocationLimitReached+94
 5: 00007FF649085CAD v8::SharedArrayBuffer::Externalize+781
 6: 00007FF648F2907C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1468
 7: 00007FF648F26194 v8::internal::Heap::CollectGarbage+4244
 8: 00007FF648F23B10 v8::internal::Heap::AllocateExternalBackingStore+2000
 9: 00007FF648F48696 v8::internal::Factory::NewFillerObject+214
10: 00007FF648C7AB65 v8::internal::DateCache::Weekday+1797
11: 00007FF6491293C1 v8::internal::SetupIsolateDelegate::SetupHeap+494417
12: 00007FF6490EB013 v8::internal::SetupIsolateDelegate::SetupHeap+239523
13: 000001FD8014DF37

I read nearly every page corresponding to this error but cannot solve it.我几乎阅读了与此错误对应的每一页,但无法解决。 I tried to set an environmental variable NODE_OPTIONS to --max-old-space-size=8192 .我试图将环境变量NODE_OPTIONS设置为--max-old-space-size=8192 I tried to run the program with the command: node --max-old-space-size=8192 index.js我尝试使用以下命令运行该程序: node --max-old-space-size=8192 index.js

If it helps, I got my heapStatistics here:如果有帮助,我在这里得到了我的 heapStatistics:

{
  total_heap_size: 55885824,
  total_heap_size_executable: 786432,
  total_physical_size: 55885824,
  total_available_size: 10505880728,
  used_heap_size: 29100784,
  heap_size_limit: 10536091648,
  malloced_memory: 794688,
  peak_malloced_memory: 4109184,
  does_zap_garbage: 0,
  number_of_native_contexts: 1,
  number_of_detached_contexts: 0
}

How can I solve this issue?我该如何解决这个问题?

Based on your original code in your previous question, the answer there missed one very important detail...根据您在上一个问题中的原始代码,那里的答案遗漏了一个非常重要的细节......

while(true) {
    func();
}

Also, the code in the answer won't even parse!!!此外,答案中的代码甚至不会解析!!! But you must've figured that out for yourself or you wouldn't have the issue you are having (a missing ) in that answer)但是您必须自己解决这个问题,否则您将不会遇到您在该答案中遇到的问题(缺少)

Now that you are no longer waiting for the calc function "inline", that loop will runaway using as much resources until bang it dies from lack of memory or whatever现在您不再等待calc function“内联”,该循环将使用尽可能多的资源失控,直到它因缺少memory或其他原因而死

You need to wait for func to finish each "round" of calc s您需要等待func完成calc的每一“轮”

So - perhaps something like所以 - 也许像

async function func() {
    const promises = routes.map(route => calc(route, amount_wei));
    const completedItems = await Promise.all(promises);
    completedItems.forEach(function (result_amount) {
        if (result_amount[5] > amount_start * 1) {
            console.log("Good Trade");
        }
    });
}

Now, you must现在,你必须

while (true) {
    await func();
}

Though, there's probably better way to do so, so that each route can work independently, re-running calc - but that's an exercise for you if you believe you want to do it that way虽然,可能有更好的方法来做到这一点,这样每条route都可以独立工作,重新运行calc - 但如果你相信你想那样做的话,这对你来说是一个练习

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

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