You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Openshift Online构建Angular应用时内存不足导致构建失败

Troubleshooting Angular 9 Build OOM (Error 137) on OpenShift Online S2I

Hey Jonathan, I’ve run into similar memory issues with Angular builds in containerized environments before—let’s break down why this is happening and how to fix it, since local vs. container environments have key differences that drive memory usage up:

Why This Happens (Even With 1GB Pod Limit)

  • Node.js Heap Cap Isn’t Maximized: By default, Node.js sets an internal heap memory limit, but in a container with a 1GB hard limit, Node might not automatically adjust to use the full available space. Worse, the container runtime itself uses some memory (for OS processes, etc.), so your build process only gets ~700-800MB of usable RAM instead of the full 1GB.
  • No Build Cache: Local builds reuse node_modules and Angular’s build cache, but S2I starts fresh every time. Reinstalling dependencies and compiling everything from scratch uses way more memory than incremental local builds.
  • Package Manager Version Mismatch: If the OpenShift Node.js S2I image uses a newer npm (v7+) while you’re on npm v6 locally, npm v7’s dependency resolution uses significantly more memory during npm install.
  • Angular’s Default Build Settings: Dev-mode builds generate source maps and skip optimizations, which eat up memory. Plus, ng build auto-scales parallel tasks based on CPU cores—if the container has fewer cores, task scheduling overhead can bloat memory usage.

Fixes to Try (Ordered by Ease of Implementation)

1. Bump Node.js Heap Memory for the Build

Tell Node to use as much memory as possible within the pod’s limit by modifying your package.json build script. Add the --max_old_space_size flag to give Angular room to breathe:

"scripts": {
  "build": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build"
}

Don’t worry about the 8GB number—Node will cap itself at the pod’s 1GB limit, but this removes Node’s internal heap restriction.

2. Enable S2I Build Caching

OpenShift lets you cache node_modules and build artifacts between builds, which cuts down on memory-heavy reinstalls and recompiles. Here’s how to set it up via the Web Console:

  • Go to your Build Config → Build Settings
  • Scroll to "Build Cache" and toggle it on
  • Create a small PVC (1GB is enough) to store the cache
    Next build will populate the cache, and subsequent builds will use it—you’ll see a huge drop in memory usage and build time.

3. Optimize Angular’s Build Configuration

  • Disable Source Maps: If you don’t need them for debugging, add "sourceMap": false to the production configuration in angular.json—this reduces memory overhead during compilation.
  • Use Production Mode: Even for testing, ng build --prod enables tree-shaking and compression, which uses less memory than dev-mode builds (since it strips out unnecessary code earlier).
  • Limit Parallel Workers: Force Angular to use fewer parallel tasks to reduce memory contention. Update your build script:
"scripts": {
  "build": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --max-workers=1"
}

4. Align Package Manager Versions

Make sure the S2I image uses the same npm version as your local machine. Add a .npmrc file to your repo with:

engine-strict=true

And update package.json to specify your npm version:

"engines": {
  "npm": "6.x"
}

This tells the S2I build to use npm v6, matching your local setup and avoiding memory-heavy dependency resolution from newer versions.

5. Diagnose Exact Memory Usage (If You’re Curious)

To confirm where memory is being eaten up, you can tweak your build command to log memory stats. For example, add free -m before the build to check available RAM, and ps aux during the build to monitor processes:

"scripts": {
  "build": "free -m && node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build && ps aux"
}

Check the build logs to see how much memory is available when the build starts, and which processes are using the most RAM.


内容的提问来源于stack exchange,提问作者Jonathan Trew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 08:22:33