node.jsubuntunpmnpm-buildaws-ec2-instance-connect

Getting Error - allocation failure scavenge might not succeed & Allocation failed - JavaScript heap out of memory


React Version: 18.2.0
Node.js version: v16.18.0
Npm version: 8.19.2

Problem

I am getting error in my AWS EC2 t2.micro Ubuntu OS while building my react-app.

Whenever I try to use npm run build

I get following error

<--- Last few GCs --->

[1623:0x6338df0]    66411 ms: Scavenge 471.0 (491.0) -> 467.5 (491.2) MB, 2.1 / 0.0 ms  (average mu = 0.211, current mu = 0.097) allocation failure
[1623:0x6338df0]    66444 ms: Scavenge 471.2 (491.2) -> 467.8 (491.5) MB, 2.2 / 0.0 ms  (average mu = 0.211, current mu = 0.097) allocation failure
[1623:0x6338df0]    67665 ms: Mark-sweep 471.4 (491.5) -> 464.1 (491.7) MB, 1191.7 / 0.0 ms  (average mu = 0.269, current mu = 0.324) allocation failure scavenge might not succeed

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0xb06730 node::Abort() [/usr/bin/node]
2: 0xa1b6d0  [/usr/bin/node]
3: 0xce1dd0 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/bin/node]
4: 0xce2177 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/bin/node]
5: 0xe997e5  [/usr/bin/node]
6: 0xe9a2c6  [/usr/bin/node]
7: 0xea87ee  [/usr/bin/node]
8: 0xea9230 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/bin/node]
9: 0xeac1ae v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/usr/bin/node]
10: 0xe6d6ea v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/usr/bin/node]
11: 0x11e6456 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/usr/bin/node]
12: 0x15da0d9  [/usr/bin/node]

Already tried solutions

After reading lot of solutions online I tried increasing size of cache using

export NODE_OPTIONS=--max-old-space-size=2048 Still same error (also tried for 4,8,16 GB)

Note

npm run build works on my local machine with following configuration.

MAC OS montere
Node version: v16.13.1
Npm version: 8.3.0
React: 18.2.0

Is the possible problem with AWS EC2 t2.micro or node/npm versions?

It should have build a optimal production build folder for deploying


Solution

  • Updated t2.micro to t2.medium and now its working.