node.js - 防止节点在 600 MB 内存限制的 docker build 中耗尽内存
问题描述
我正在运行docker build
构建的内存和 CPU 的限制。为了不超出构建的 CPU 和内存限制,我还将 Node 的堆大小限制为 325 MB。这是docker build
命令。
docker build --build-arg NODE_OPTIONS=--max-old-space-size=325 \
--memory=600m --memory-swap=-1 \
--cpu-period=100000 --cpu-quota=50000 \
--no-cache --tag farm_app_image:latest --file Dockerfile .
构建资源摘要
- 节点 JS 堆限制:325 MB
- 构建内存:600 MB,无限使用交换文件。
- 构建 CPU 时间:50%
尽管节点堆限制低于构建内存,并且尽管有无限交换,但该步骤npm run build
的内存不足。react-scripts build
错误输出
> react-scripts build
Creating an optimized production build...
EXEC : FATAL error : Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory [/app/FarmLandLasanga/FarmLandLasanga.csproj]
<--- Last few GCs --->
[186:0x5e6ca40] 143688 ms: Mark-sweep 319.3 (329.2) -> 319.2 (329.9) MB, 1278.3 / 0.0 ms (average mu = 0.118, current mu = 0.002) allocation failure scavenge might not succeed
[186:0x5e6ca40] 145181 ms: Mark-sweep 320.3 (329.9) -> 320.2 (331.2) MB, 1488.0 / 0.1 ms (average mu = 0.060, current mu = 0.003) allocation failure scavenge might not succeed
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x1391439]
1: StubFrame [pc: 0x1316d29]
Security context: 0x154b83ac08d1 <JSObject>
2: /* anonymous */ [0x14bbf66e0a09] [/app/FarmLandLasanga/ClientAppTypeScript/node_modules/webpack-sources/node_modules/source-map/lib/source-node.js:~86] [pc=0xa4979a73d50](t
his=0x1e34cc96a351 <JSFunction SourceNode (sfi = 0xd8bc3128ee1)>,0x307dd6e664f1 <Object map = 0xb3550d64299>)
3: arguments adaptor frame: 3->1
4:...
Writing Node.js report to file: report.20191116.195427.186.0.001.json
Node.js report completed
1: 0x9e9f40 node::Abort() [node]
2: 0x9ec192 node::OnFatalError(char const*, char const*) [node]
3: 0xb4611e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xb46499 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xcf3535 [node]
6: 0xcf3bc6 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
7: 0xd003fa v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
8: 0xd01305 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
9: 0xd03dac v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
10: 0xcca66b v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
11: 0x100eb9e v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
12: 0x1391439 [node]
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! farm-land-lasagna@0.1.0 build: `react-scripts --max_old_space_size=325 build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the farm-land-lasagna@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/2019-11-16T19_54_27_392Z-debug.log
问题
我可以对docker build
命令进行哪些其他更改(如果有的话)以使其在600 MB
限制下成功?
参考
- https://docs.docker.com/config/containers/resource_constraints/
- https://erikcorry.blogspot.com/2012/11/memory-management-flags-in-v8.html
额外的尝试
- 随着
--memory=900m
构建成功。 react-scripts --max_old_space_size=325 build
Docker 内部失败。react-scripts --max_old_space_size=325 build
在 Docker 之外成功。
解决方案
你能改变你的基本图像吗?我认为这会有所帮助。此内存泄漏问题出现在 NodeJS v10 和 v12 中,只需使用最新的 LTS 版本 (v14) 以及图像的 alpine 版本。
推荐阅读
- google-cloud-firestore - Firestore:给定一个文档 ID 列表,检查哪些记录实际存在的最便宜和最优雅的方法是什么?
- java - 查找比较次数
- google-apps-script - 我无法从谷歌应用脚本发送超过 100 封邮件。如何增加 App 脚本的邮件发送限制?
- reactjs - 从文件夹动态导入图像
- postgresql - 是否可以将 Debezium 与不在 docker 中的 Postgresql 连接起来?
- python - Python 和 PEP8 中的私有属性
- javascript - ReferenceError:消息未定义,请解决此问题
- java - Android 无法识别 Bitmap.getColor,关于版本,这是为什么呢?
- javascript - 如何指定个人用户列表以访问 Google 应用脚本中的特定文件?
- python - pyautogui 和 tkinter 中的确认和主循环