第一种情况

umi 项目在发布部署系统报内存不足,导致错误编译失败
image.png

这是因为 CI/CD 或 Jenkins/Docker 等流程系统,terser-webpack-plugin 读取的 cpu 数量并非真实的,多进程打包就回导致分配过多的进程,导致占用内存爆掉出错。

可以通过 top -c -bw 500 -o %MEM & 查看进程数,检查打包时,分配了多少并发进程,内存大概占用多少

  1. # 会看到非常多 processChild 进程,导致内存使用失控
  2. /home/jenkins/agent/workspace/xxx-ui-sit/node_modules/jest-worker/build/workers/processChild.js
  3. # 非常多的此进程 此处省略(因为部署系统读到的 cpu 数并非真实的)

umi 项目已经有逻辑修复此问题,process.env.SIGMA_MAX_PROCESSORS_LIMIT 可用来指定真实核数。

@umijs/bundler-webpack/src/getConfig/getConfig.ts

umi@3.4.23 版本中解决了此问题,相关 issue https://github.com/umijs/umi/pull/6647

代码如下

  1. // 兼容内部流程系统,读到的 cpu 数并非真实的
  2. // 使用 SIGMA_MAX_PROCESSORS_LIMIT 指定真核数
  3. parallel: process.env.SIGMA_MAX_PROCESSORS_LIMIT
  4. ? parseInt(process.env.SIGMA_MAX_PROCESSORS_LIMIT, 10)
  5. : true,

第二种情况

有时遇到 Error: The service was stopped 直接 build 报错了,没有更多有用的信息,

  1. [2021-07-28T08:44:38.463Z] > umi build
  2. [2021-07-28T08:44:38.463Z]
  3. [2021-07-28T08:44:42.671Z] [info] [webpackbar] Compiling Webpack
  4. [2021-07-28T08:45:50.564Z] [BABEL] Note: The code generator has deoptimised the styling of /home/jenkins/agent/workspace/xxx-ui-sit/node_modules/@antv/g6/dist/g6.min.js as it exceeds the max of 500KB.
  5. [2021-07-28T08:47:42.063Z] Error: The service was stopped
  6. [2021-07-28T08:47:42.063Z] at sendRequest (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/esbuild/lib/main.js:1020:33)
  7. [2021-07-28T08:47:42.063Z] at responseCallbacks.set (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/esbuild/lib/main.js:542:9)
  8. [2021-07-28T08:47:42.063Z] at Socket.afterClose (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/esbuild/lib/main.js:520:7)
  9. [2021-07-28T08:47:42.063Z] at Socket.emit (events.js:194:15)
  10. [2021-07-28T08:47:42.063Z] at endReadableNT (_stream_readable.js:1125:12)
  11. [2021-07-28T08:47:42.063Z] at process._tickCallback (internal/process/next_tick.js:63:19)
  12. [2021-07-28T08:47:42.063Z] build failed
  13. [2021-07-28T08:47:42.063Z] Error: build failed
  14. [2021-07-28T08:47:42.063Z] at compiler.run (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/@umijs/bundler-webpack/lib/index.js:138:27)
  15. [2021-07-28T08:47:42.063Z] at finalCallback (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/MultiCompiler.js:254:12)
  16. [2021-07-28T08:47:42.063Z] at runWithDependencies.err (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/MultiCompiler.js:275:14)
  17. [2021-07-28T08:47:42.063Z] at /home/jenkins/agent/workspace/xxx-ui-sit/node_modules/neo-async/async.js:2830:7
  18. [2021-07-28T08:47:42.063Z] at done (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/neo-async/async.js:2925:13)
  19. [2021-07-28T08:47:42.063Z] at err (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/MultiCompiler.js:186:23)
  20. [2021-07-28T08:47:42.063Z] at compiler.run (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/MultiCompiler.js:267:15)
  21. [2021-07-28T08:47:42.063Z] at finalCallback (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/Compiler.js:257:39)
  22. [2021-07-28T08:47:42.063Z] at onCompiled (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/Compiler.js:265:20)
  23. [2021-07-28T08:47:42.063Z] at compilation.seal.err (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/Compiler.js:676:23)
  24. [2021-07-28T08:47:42.063Z] at hooks.optimizeChunkAssets.callAsync.err (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/webpack/lib/Compilation.js:1411:14)
  25. [2021-07-28T08:47:42.063Z] at _promise1.then._err1 (eval at create (/home/jenkins/agent/workspace/xxx-ui-sit/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:22:1)
  26. [2021-07-28T08:47:42.063Z] at process._tickCallback (internal/process/next_tick.js:68:7)
  27. [2021-07-28T08:47:42.063Z] npm ERR! code ELIFECYCLE
  28. [2021-07-28T08:47:42.063Z] npm ERR! errno 1
  29. [2021-07-28T08:47:42.063Z] npm ERR! xxx@ build: `umi build`
  30. [2021-07-28T08:47:42.063Z] npm ERR! Exit status 1

可以通过以下方法来处理:

  1. # 查看内存情况
  2. free -m
  3. # 约束 nodejs 内存使用
  4. # 部署系统限制使用的内存,本质上操作的就是下面这个
  5. export NODE_OPTIONS="--max-old-space-size=4096"
  6. npm run build

扩展阅读: