Ionic构建Android时出现CALL_AND_RETRY_LAST内存分配失败错误求助
Hey there, I’ve dealt with this exact JavaScript heap out of memory error in Ionic Android builds plenty of times—let’s walk through the fixes that actually worked for me and other devs:
Increase Node.js memory limit directly in your build command
When running your build command, add the--max-old-space-sizeflag to allocate more memory to Node.js. You can adjust the value based on your system’s available RAM—8192 equals 8GB, which is a common starting point:node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build --prodIf 8GB feels too heavy, try 4096 (4GB) first.
Add a custom npm script for easier builds
Instead of typing the long command every time, update yourpackage.jsonwith a dedicated script:"scripts": { "build:android:prod": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build --prod" }Then just run
npm run build:android:prodwhenever you need to build for Android.Clean up your project to eliminate corrupted files
- Delete the
node_modulesfolder,package-lock.json(oryarn.lock), andwwwfolder - Run
npm installto reinstall dependencies from scratch - Clear Ionic’s cache with
ionic cache clear
Corrupted dependencies or cached build files often make memory issues worse, so this fresh start can work wonders.
- Delete the
Trim down your project’s build size
- Compress large images, fonts, or videos in your
src/assetsfolder—unoptimized media bloats the build process - Use tools like
webpack-bundle-analyzerto spot unused libraries or oversized code chunks that are being bundled unnecessarily
Reducing the overall load on Node.js can prevent it from running out of memory.
- Compress large images, fonts, or videos in your
Update your Ionic tooling and dependencies
Outdated versions of Ionic, app-scripts, or Cordova sometimes have memory leaks or bugs that trigger this error. Run these commands to get the latest versions:npm install @ionic/app-scripts@latest --save-dev npm install ionic@latest -g cordova platform update android
If none of these steps resolve the issue, sharing your full ionic info output would help narrow things down further—but these fixes take care of 90% of the heap out-of-memory cases I’ve encountered.
内容的提问来源于stack exchange,提问作者Green Onyeji

