最近开发的一个比较大型的App时打生产环境包是出现内存不足导致打包失败的问题

错误信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ ionic cordova build ios --prod --verbose
[20:08:46] build prod started ...
[20:08:46] clean started ...
[20:08:46] clean finished in 1 ms
[20:08:46] copy started ...
[20:08:46] ngc started ...
[20:09:19] ngc finished in 32.84 s
[20:09:19] preprocess started ...
[20:09:19] deeplinks started ...
[20:09:22] deeplinks finished in 3.67 s
[20:09:22] optimization started ...
[20:09:39] copy finished in 52.56 s
[20:10:04] optimization finished in 41.73 s
[20:10:04] preprocess finished in 45.40 s
[20:10:04] webpack started ...
<--- Last few GCs --->
402717 ms: Mark-sweep 1332.3 (1437.1) -> 1331.6 (1437.1) MB, 1137.5 / 0.0 ms [allocation failure] [GC in old space requested].
403827 ms: Mark-sweep 1331.6 (1437.1) -> 1331.5 (1437.1) MB, 1109.4 / 0.0 ms [allocation failure] [GC in old space requested].
404969 ms: Mark-sweep 1331.5 (1437.1) -> 1340.4 (1421.1) MB, 1141.3 / 0.0 ms [last resort gc].
406115 ms: Mark-sweep 1340.4 (1421.1) -> 1349.5 (1421.1) MB, 1145.4 / 0.0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x3253a08cfb51
2: /* anonymous */ [/Users/fan2net/truemerger/xincaidong/node_modules/source-map/lib/source-node.js:~80] [pc=0x40b1c23c541] (this=0x9c85c680801 <JS Function SourceNode (SharedFunctionInfo 0xd26940f131)>,mapping=0x392f564c03b1 <an Object with map 0xc6fc3d4d9f9>)
3: arguments adaptor frame: 3->1
4: InnerArrayForEach(aka InnerArrayForEach) [native array.js:~935] [pc=0x40b1becab8b] (th...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
1: node::Abort() [/usr/local/bin/node]
2: node::FatalException(v8::Isolate*, v8::Localv8::Value, v8::Localv8::Message) [/usr/local/bin/node]
3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
4: v8::internal::Factory::NewUninitializedFixedArray(int) [/usr/local/bin/node]
5: v8::internal::(anonymous namespace)::ElementsAccessorBase<v8::internal::(anonymous

解决办法

  1. package.json文件的scripts中添加

    1
    2
    3
    4
    5
    {
    script:{
    "ionic:build": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build",
    }
    }
  2. 构建ionic

    1
    npm run ionic:build --prod
  3. 构建codrova

    1
    2
    cordova build android --release
    cordova build ios --release