JavaScript 当 运行 nodejs 应用程序在 ubuntu 服务器中时堆内存不足

JavaScript heap out of memory when running nodejs app inside ubuntu server

我在 Ubuntu 20.x 服务器中克隆了一个 expressjs api。

我尝试 运行 以下脚本:tsc && node build/app.js,但几秒钟后我收到以下错误:

<--- Last few GCs --->

[37431:0x4ba77f0] 23371 ms: Scavenge 482.9 (492.8) -> 482.5 (493.0) MB, 6.3 / 0.0 ms (average mu = 0.233, current mu = 0.195) allocation failure

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0xb09980 node::Abort() [node] 2: 0xa1c235 node::FatalError(char const*, char const*) [node] 3: 0xcf77be v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node] 4: 0xcf7b37 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node] 5: 0xeaf3d5 [node] 6: 0xeafeb6 [node] 7: 0xebe3de [node] 8: 0xebee20 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node] 9: 0xec1d15 v8::internal::Heap::HandleGCRequest() [node] 10: 0xe4f3b7 v8::internal::StackGuard::HandleInterrupts() [node] 11: 0x11fb775 v8::internal::Runtime_StackGuard(int, unsigned long*, v8::internal::Isolate*) [node] 12: 0x15f0a99 [node] Aborted (core dumped)

我关注过一些帖子说要增加堆大小,我通过将它添加到我的节点脚本 node --max-old-space-size-8192 build/app.js 一直做到了 8GB,但这没有帮助。

我在 Ubuntu 20.x 服务器中使用 Node 版本 16.14.0。

这里的问题是 AWS t2micro 对于 运行 Nodejs 应用程序来说太小了,但 t2small 应该可以解决问题。