Node.js Promise.all-JavaScript堆内存不足

yrdbyhpb  于 2022-10-02  发布在  Node.js
关注(0)|答案(1)|浏览(212)

我尝试并行获取node.js中的一些数据,因此与顺序获取相比,程序速度更快。我是这样做的:

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

我得到了代码here的帮助。

我确实有10条路由,所以for循环应该运行10次迭代。但无论我尝试什么,我都得到了一个错误:

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

我几乎阅读了与这个错误对应的每一页,但无法解决它。我尝试将环境变量NODE_OPTIONS设置为--max-old-space-size=8192。我尝试使用以下命令运行程序:node --max-old-space-size=8192 index.js

如果有帮助的话,我在这里得到了我的堆统计数据:

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

我怎么才能解决这个问题呢?

iyfamqjs

iyfamqjs1#

根据您在上一个问题中的原始代码,答案遗漏了一个非常重要的细节……

while(true) {
    func();
}

而且,答案中的代码甚至不会进行解析!但你必须自己想出来,否则你就不会有这个问题(答案中遗漏了一个))

现在您不再等待calc函数“内联”,该循环将使用相同的资源失控,直到它因内存不足或其他原因而死亡。

您需要等待func完成calc的每一轮

所以-也许就像这样

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

现在,你必须

while (true) {
    await func();
}

不过,可能还有更好的方法来做到这一点,这样每个route都可以独立工作,重新运行calc--但如果您认为自己想这样做,那么这是一种练习

相关问题