脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

AWS EC2で”JavaScript heap out of memory”エラーが出たら

AWS EC2で”JavaScript heap out of memory”エラーが出たときの対処法です
主にt2.microとかメモリ容量の小さいインスタンスでnuxtとかnode使ってると出現する

こんなエラー

<--- Last few GCs --->

[5746:0x42f2e10]    11947 ms: Mark-sweep 101.0 (114.6) -> 100.2 (113.6) MB, 140.1 / 0.0 ms  (+ 0.3 ms in 1 steps since start of marking, biggest step 0.3 ms, walltime since start of marking 182 ms) (average mu = 0.294, current mu = 0.434) low memory notif[5746:0x42f2e10]    12099 ms: Mark-sweep 100.2 (113.6) -> 100.2 (113.6) MB, 152.0 / 0.0 ms  (average mu = 0.168, current mu = 0.000) low memory notification GC in old space requested

<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x1098ebb9e549 <JSObject>
    0: builtin exit frame: new ArrayBuffer(aka ArrayBuffer)(this=0x1ff267182801 <the_hole>,0x3c9d355d9001 <Number 6.41975e+06>,0x1ff267182801 <the_hole>)

    1: ConstructFrame [pc: 0x3e29d418d145]
    2: StubFrame [pc: 0x3e29d420b900]
    3: createUnsafeArrayBuffer(aka createUnsafeArrayBuffer) [0x3bbd5485d531] [buffer.js:119] [bytecode=0x365108798601 offset=25](this=0x1ff2671826f1 <undefined>,s...

FATAL ERROR: Committing semi space failed. Allocation failed - JavaScript heap out of memory
 1: 0x8c02c0 node::Abort() [node]
 2: 0x8c030c  [node]
 3: 0xad15de v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xad1814 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xebe752  [node]
 6: 0xecb0f2  [node]
 7: 0xecb2b4 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 8: 0xecc7aa v8::internal::Heap::CollectAllAvailableGarbage(v8::internal::GarbageCollectionReason) [node]
 9: 0xae6cc5 v8::Isolate::LowMemoryNotification() [node]
10: 0x8bfdad node::ArrayBufferAllocator::Allocate(unsigned long) [node]
11: 0xfd88e3 v8::internal::JSArrayBuffer::SetupAllocatingData(v8::internal::Handle<v8::internal::JSArrayBuffer>, v8::internal::Isolate*, unsigned long, bool, v8::internal::SharedFlag) [node]
12: 0xb64514  [node]
13: 0xb664b3 v8::internal::Builtin_ArrayBufferConstructor(int, v8::internal::Object**, v8::internal::Isolate*) [node]
14: 0x3e29d41dc17d

対処法

sudo su
dd if=/dev/zero of=/swapfile bs=1M count=1024
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile