-
빌드시 메모리 부족으로 빌드 실패하는 현상 해결기록이라도 하자 2023. 10. 7. 19:57
* 2023년 8월 초에 해결한 이슈를 해결해나가는 과정을 기록
build할 때 번들 파일이 일정 이상 커지면, netlify에서 빌드가 실패하는 오류가 생기기 시작했고내가 생각했을 때 임계치가 약 24MB 정도 였던 것 같다. (webpack-bundle-analyzer에서 나온 raw 파일 크기 기준)
<--- Last few GCs ---> [2281:0x4f9f1c0] 282954 ms: Mark-sweep 2035.6 (2086.5) -> 2033.7 (2088.3) MB, 1709.7 / 0.0 ms (average mu = 0.244, current mu = 0.033) allocation failure scavenge might not succeed [2281:0x4f9f1c0] 285268 ms: Mark-sweep 2037.6 (2088.3) -> 2035.0 (2097.5) MB, 2307.3 / 0.0 ms (average mu = 0.114, current mu = 0.003) allocation failure scavenge might not succeed <--- JS stacktrace ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb090e0 node::Abort() [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 2: 0xa1b70e [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 3: 0xce1a20 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 4: 0xce1dc7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 5: 0xe99435 [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 6: 0xea90fd v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 7: 0xeabdfe v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 8: 0xe6d072 v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 9: 0xe65684 v8::internal::FactoryBase<v8::internal::Factory>::AllocateRawWithImmortalMap(int, v8::internal::AllocationType, v8::internal::Map, v8::internal::AllocationAlignment) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 10: 0xe676e1 v8::internal::FactoryBase<v8::internal::Factory>::NewRawTwoByteString(int, v8::internal::AllocationType) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 11: 0x10f8915 v8::internal::String::SlowFlatten(v8::internal::Isolate*, v8::internal::Handle<v8::internal::ConsString>, v8::internal::AllocationType) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 12: 0x10ef6aa v8::internal::StringTable::LookupString(v8::internal::Isolate*, v8::internal::Handle<v8::internal::String>) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 13: 0x11f1ad6 v8::internal::Runtime_HasProperty(int, unsigned long*, v8::internal::Isolate*) [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] 14: 0x15d9e59 [/opt/hostedtoolcache/node/16.20.2/x64/bin/node] error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. Error: Process completed with exit code 1.
보통 이 오류가 발생했고, 대부분은 어떻게서든 코드 최적화를 통해 번들 사이즈를 줄이는 식으로 해결했었다.
그러나 코드량은 계속 증가할 것이기 때문에 어디까지나 일시적인 해결방안이었다.
메모리 문제라고 하는 것 같아서 빌드 스크립트에 --max-old-space-size=4096을 추가했지만, 계속해서 빌드는 실패했다. 숫자를 좀 더 크게 올려줘도 마찬가지였다. 아래 코드는 빌드 커맨드에 옵션을 추가한 것이다. (이렇게 하면 안됨)
yarn --max-old-space-size=8192 {{ build_command }}
그렇다고 프로그램에 메모리 누수가 발생했냐하면 그건 아닌 것 같았다.
그래서 계속 해결방안을 찾지 못하고 급한대로 Sentry 옵션을 꺼서 배포를 하였다.
그러다가 결국 찾게된 방법은 옵션의 순서를 변경하는 것이었다. (GPT야 고맙다...)
yarn을 실행하기 전에 NODE_OPTIONS의 환경변수를 설정해야만 Node.js에서 사용할 수 있는 메모리가 증가한다.
GPT의 도움을 받았다 NODE_OPTIONS=--max-old-space-size=8192 yarn {{ build_command }}
이랬더니 빌드가 성공했다!
빌드 스크립트를 변경하고 다시 sentry를 활성화시켰다.'기록이라도 하자' 카테고리의 다른 글
Tailwind 동적 클래스 생성이 동작하지 않을 때 (0) 2023.12.20 Netlify Git Branch 이름 기준으로 배포 무시하기 (netlify ignore) (1) 2023.10.17 드러커 엑서사이즈 (0) 2023.10.02 장고 다국어 i18n 적용 (0) 2023.09.22 leaflet-rotate로 leaflet 지도의 각도를 변경해보자. (1) 2023.08.14