美文网首页java工程师常用工具
react项目build时发生“v8::internal::V8

react项目build时发生“v8::internal::V8

作者: 小马将过河 | 来源:发表于2018-07-05 17:19 被阅读0次
    shouji.jpg
    >>>首发于我的博客<<<

    现象

    最近工程里有个用Ant Design Pro搭建的前后端分离的后台管理系统,一直使用jenkins自动部署的,但是今天一不小心build失败了。虽然很快就搞定了,但是还是记录一下,好记性不如烂笔头。

    问题出现

    构建失败信息是这样的:

    <pre class="console-output" style="box-sizing: border-box; white-space: pre-wrap; word-wrap: break-word; margin: 0px; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Started by user [超级管理员](http://jenkins.haier.haiqiang.xin:7776/user/admin)
    Building in workspace /var/lib/jenkins/workspace/chuangke_pre_portal_web_auto_deploy
     > git rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
     > git config remote.origin.url git@git.3vyd.com:portalmanage.git # timeout=10
    Fetching upstream changes from git@git.3vyd.com:portalmanage.git
     > git --version # timeout=10
     > git fetch --tags --progress git@git.3vyd.com:portalmanage.git +refs/heads/*:refs/remotes/origin/*
     > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
     > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
    Checking out Revision c9a9d48ae6453a8d84734920f8718abec5598f2a (refs/remotes/origin/master)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f c9a9d48ae6453a8d84734920f8718abec5598f2a
    Commit message: "直播统计"
     > git rev-list --no-walk 34ede46c51b5a223746b1c07d0845952d7d0eefd # timeout=10
    [chuangke_pre_portal_web_auto_deploy] $ /bin/sh -xe /tmp/jenkins1426166608252484443.sh
    + yarn
    yarn install v1.3.2
    [1/4] Resolving packages...
    success Already up-to-date.
    Done in 2.31s.
    + REACT_APP_BRANCH=PRE
    + yarn build
    yarn run v1.3.2
    $ node scripts/build.js
    Creating an optimized production build...
    
    <--- Last few GCs --->
    
      780502 ms: Mark-sweep 1323.4 (1408.7) -> 1317.3 (1408.7) MB, 955.6 / 0.0 ms [allocation failure] [GC in old space requested].
      781304 ms: Mark-sweep 1317.3 (1408.7) -> 1317.3 (1410.7) MB, 802.4 / 0.0 ms [allocation failure] [GC in old space requested].
      782171 ms: Mark-sweep 1317.3 (1410.7) -> 1326.8 (1403.7) MB, 866.5 / 0.0 ms [last resort gc].
      782958 ms: Mark-sweep 1326.8 (1403.7) -> 1336.9 (1403.7) MB, 787.3 / 0.0 ms [last resort gc].
    
    <--- JS stacktrace --->
    
    ==== JS stack trace =========================================
    
    Security context: 0x1116596cf781 <JS Object>
        1: stringify [native json.js:~176] [pc=0x1ab7d7e562d3] (this=0x1116596c92a1 <a JSON with map 0xa5d18e09b09>,E=0x27fe2de64711 <an Object with map 0x23d396b32619>,F=0x111659604381 <undefined>,S=0x111659604381 <undefined>)
        2: arguments adaptor frame: 1->3
        3: /* anonymous */(aka /* anonymous */) [/var/lib/jenkins/workspace/chuangke_pre_portal_web_auto_deploy/node_modules/webpack/lib/Sou...
    
    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
     1: node::Abort() [node]
     2: 0xdf1d9c [node]
     3: v8::Utils::ReportApiFailure(char const*, char const*) [node]
     4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
     5: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [node]
     6: v8::internal::String::SlowFlatten(v8::internal::Handle<v8::internal::ConsString>, v8::internal::PretenureFlag) [node]
     7: v8::internal::String::Flatten(v8::internal::Handle<v8::internal::String>, v8::internal::PretenureFlag) [node]
     8: v8::internal::BasicJsonStringifier::SerializeString(v8::internal::Handle<v8::internal::String>) [node]
     9: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<false>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [node]
    10: v8::internal::BasicJsonStringifier::SerializeJSArraySlow(v8::internal::Handle<v8::internal::JSArray>, unsigned int, unsigned int) [node]
    11: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<true>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [node]
    12: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<false>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [node]
    13: v8::internal::BasicJsonStringifier::Stringify(v8::internal::Handle<v8::internal::Object>) [node]
    14: v8::internal::Runtime_BasicJSONStringify(int, v8::internal::Object**, v8::internal::Isolate*) [node]
    15: 0x1ab7ca0092a7
    error Command failed with signal "SIGABRT".
    info Visit [https://yarnpkg.com/en/docs/cli/run](https://yarnpkg.com/en/docs/cli/run) for documentation about this command.
    Build step 'Execute shell' marked build as failure
    Sending e-mails to: marvin.ma@3vyd.com damon.shen@3vyd.com
    Finished: FAILURE</pre>
    

    解决思路

    1、Jenkins服务器系统内存不够
    看了看jenkins所在服务器的内存,在jenkins执行这个job的时候,内存最高也就140%,因为服务器是Centos 4核16G的配置,用free查看,可用内存仍然9G多。当时没有截图,所以就不贴图了,但是肯定不是说服务器内存吃紧导致的。

    2、排除了服务器本身内存小的问题,考虑本次build新进的代码的问题,看了一下就两个commit,因为网上有说是调用file.write的时候处理不当,会导致该问题的产生的,正好本次build有一个commit是添加了富文本编辑器。
    但是检查一圈好像没有问题。

    3、框架问题,限制了内存。

    解决方案

    最后通过https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory一文找到了解决方案。先做本地部署尝试,修改该job的build命令,将原来的

    yarn
    REACT_APP_BRANCH=PRE yarn build

    改成了

    yarn
    REACT_APP_BRANCH=PRE node --max-old-space-size=4096 scripts/build.js


    jenkins build shell

    重新build,success。

    然后因为该项目分开发,测试、生产等多个环境,因此打算在代码框架里直接作为配置项,具体的做法是在package.json的build命令前加上指定内存的参数--max-old-space-size=4096。将原来的:

    "scripts": {
        "start": "node scripts/start.js",
        "build": "node scripts/build.js",
        "test": "node scripts/test.js --env=jsdom"
      }
    

    修改为:

      "scripts": {
        "start": "node scripts/start.js",
        "build": "node --max-old-space-size=4096 scripts/build.js",
        "test": "node scripts/test.js --env=jsdom"
      },
    

    提交代码,jenkins修改回原来的yarn start,重新build, SUCCESS。
    一劳永逸。

    相关文章

      网友评论

        本文标题:react项目build时发生“v8::internal::V8

        本文链接:https://www.haomeiwen.com/subject/tmipuftx.html