Ubuntu 致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足

问题描述

我正在按照本教程通过 Ubuntu 在 AWS EC2 实例上部署我的应用程序:

https://itnext.io/deploy-a-mongodb-expressjs-reactjs-nodejs-mern-stack-web-application-on-aws-ec2-2a0d8199a682

一切顺利,直到我跑

sudo npm run build --max-old-space-size=16384

显示以下错误

Creating an optimized production build...

<--- Last few GCs --->

[15235:0x60610e0]    37407 ms: Scavenge 484.2 (492.9) -> 482.6 (492.9) MB,24.3 / 0.0 ms  (average mu = 0.289,current mu = 0.276) allocation failure
[15235:0x60610e0]    37616 ms: Scavenge (reduce) 484.5 (492.9) -> 482.9 (494.7) MB,11.6 / 0.0 ms  (average mu = 0.289,current mu = 0.276) allocation failure
[15235:0x60610e0]    37721 ms: Scavenge (reduce) 483.7 (491.7) -> 483.0 (492.4) MB,19.8 / 0.0 ms  (average mu = 0.289,current mu = 0.276) allocation failure


<--- JS stacktrace --->

Fatal error: Ineffective mark-compacts near heap limit Allocation Failed - JavaScript heap out of memory
 1: 0xa04200 node::Abort() [/usr/local/bin/node]
 2: 0x94e4e9 node::FatalError(char const*,char const*) [/usr/local/bin/node]
 3: 0xb797be v8::Utils::ReportOOMFailure(v8::internal::Isolate*,char const*,bool) [/usr/local/bin/node]
 4: 0xb79b37 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*,bool) [/usr/local/bin/node]
 5: 0xd343c5  [/usr/local/bin/node]
 6: 0xd34f4f  [/usr/local/bin/node]
 7: 0xd42fdb v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace,v8::internal::GarbageCollectionReason,v8::GCCallbackFlags) [/usr/local/bin/node]
 8: 0xd45695 v8::internal::Heap::HandleGCRequest() [/usr/local/bin/node]
 9: 0xcebcd7 v8::internal::StackGuard::HandleInterrupts() [/usr/local/bin/node]
10: 0x101df73 v8::internal::NativeRegExpMacroAssembler::CheckStackGuardState(v8::internal::Isolate*,int,v8::internal::RegExp::CallOrigin,unsigned long*,v8::internal::Code,unsigned char const**,unsigned char const**) [/usr/local/bin/node]
11: 0x131c5ec v8::internal::RegExpMacroAssemblerX64::CheckStackGuardState(unsigned long*,unsigned long,unsigned long) [/usr/local/bin/node]
12: 0x296ccae95970
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! frontend@0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the frontend@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-02-03T13_51_55_951Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! azale@1.0.0 build: ` cd frontend && npm install && npm run build && cd ..`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the azale@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-02-03T13_51_55_978Z-debug.log

如您所见,我已经尝试通过向 run build 命令添加 --max-old-space-size 来允许更大的堆大小,但我仍然无法解决此问题。

有什么建议吗?

请注意,如果这有帮助,我将使用免费层作为 EC2 实例,并且我的应用程序包含 node_modules 文件夹的大小约为 700MB。

ubuntu@ip-172-31-42-107:/var/www/azalefinal/frontend$ free -b
              total        used        free      shared  buff/cache   available
Mem:     1026154496   148914176   629264384      819200   247975936   706924544
Swap:             0           0           0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)