Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[内存溢出] 编译微信项目时内存溢出 #2647

Closed
Lionad-Morotar opened this issue Apr 1, 2019 · 8 comments
Closed

[内存溢出] 编译微信项目时内存溢出 #2647

Lionad-Morotar opened this issue Apr 1, 2019 · 8 comments
Labels
good first issue Good for newcomers

Comments

@Lionad-Morotar
Copy link

问题描述
打包时, 报 JavaScript heap out of memory 错误, 无法正常编译.
image

系统信息
👽 Taro v1.2.22

Taro CLI 1.2.22 environment info:
System:
OS: Windows 10
Binaries:
Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
Yarn: 1.13.0 - C:\Users\18062\AppData\Roaming\npm\yarn.CMD
npm: 6.9.1-next.0 - C:\Program Files\nodejs\npm.CMD

补充信息

  1. 编译这个组件的时候:
编译  组件文件  src/components/headBar/headBar.js

在任务管理器中, 发现NodeJS使用内存从1G开始飙升, 升到1.5G左右的时候就会直接报错退出.

  1. 使用 npm run fix-memory-limit 设置过项目和全局NodeJS内存大小到8192MB但是编译时内存占满1.5G仍然会报错推出.

  2. 切换到某些分支则能正常打包, 为了不影响项目进行, 目前只能先切换到一个教轻量的分支, 然后编译完成后用Git切回想要打包的分支, 编译则会自动进行下去, 不会爆内存溢出错误. 感觉像是我的项目新分支的某些页面引入组件写法有问题, 等周末有空再排查TOT

@taro-bot
Copy link

taro-bot bot commented Apr 1, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@Garfield550
Copy link
Collaborator

虽然接下来的回复对你的问题可能没什么帮助,但我想指出的是 8192MiB 是 8GiB。

@yuche
Copy link
Contributor

yuche commented Apr 2, 2019

src/components/headBar/headBar.js

这个组件的代码发到我的邮箱吧

@Lionad-Morotar
Copy link
Author

Lionad-Morotar commented Apr 2, 2019

src/components/headBar/headBar.js

这个组件的代码发到我的邮箱吧

Hi~ 问题已暂时解决~

目前为止, 我们的项目中算上分包内的页面数量约有110, headBar.js是小程序自定义的头部, 几乎每一个页面都要引用到, 这应该时导致项目打包时内存飙升的原因.

我切换到了打包没有问题的分支, 查看了一下打包时内存使用情况, 发现就算没问题的打包, NodeJS的内存也在1.4~1.5G边缘游走, 这之后, 我切换回了有问题的分支, 删除了一些代码, 就可以成功Run起来.

对, 只要删除一些代码

有没有什么办法能够增加NodeJS运行时的内存呢? 我尝试过使用increase-memory-limit这个包, 但是没有效果~

@yuche
Copy link
Contributor

yuche commented Apr 2, 2019

你可以尝试这里的解决方案:
vuejs/vue-cli#1453

@Lionad-Morotar
Copy link
Author

你可以尝试这里的解决方案:
vuejs/vue-cli#1453

刚才尝试了一下修改 @taro/cli 与全局相关文件夹中的命令, 没有效果.
这之后, 我试了一下这种方法, 也是没有效果.
image

可能和我的 NodeJS 版本之类的有关, 一会儿我降下版本试试~

@Lionad-Morotar
Copy link
Author

Lionad-Morotar commented Apr 4, 2019

你可以尝试这里的解决方案:
vuejs/vue-cli#1453

刚才尝试了一下修改 @taro/cli 与全局相关文件夹中的命令, 没有效果.
这之后, 我试了一下这种方法, 也是没有效果.
image

可能和我的 NodeJS 版本之类的有关, 一会儿我降下版本试试~

换过NodeJS版本的情况下, 但是在我的电脑上和同事的电脑上用这个方法都么得效果~(゜ー゜

/**
 *  将一个目录下所有`.cmd`文件中调用`node`命令的地方加上后缀`--max-old-space-size=8192`
 */

var fs = require ('fs')
var path = require ('path')

// const filePath = path.resolve ('C:/Users/18062/AppData/Roaming/npm')
const filePath = path.resolve ('D:/_takecloud/shopMiniProgram')
const MAX_DEEP = 9

fileDisplay (filePath)

function fileDisplay (filePath, deep = 0) {
  //根据文件路径读取文件,返回文件列表
  fs.readdir (filePath, function (err, files) {
    if (err) {
      console.warn (err)
    } else {
      files.forEach (function (filename) {
        const filedir = path.join (filePath, filename)
        fs.stat (filedir, function (eror, stats) {
          if (eror) {
            console.warn ('获取文件stats失败')
          } else {
            const isFile = stats.isFile ()
            const isDir = stats.isDirectory ()

            if (isFile) {
              const reCMD = /\.cmd$/
              if (reCMD.test(filename)) {
                console.log(filename)
                fs.readFile(filedir, 'utf8', function(err, data){
                  if (err) {
                    console.warn ('读取文件失败')
                  } else {
                    data = data.replace(/node\.exe"\s+"%/, 'node.exe --max-old-space-size=8192" "%')
                    data = data.replace(/node\s+"%/, 'node --max-old-space-size=8192 "%')
                    // data = data.replace('node --max-old-space-size=8192 "%', 'node "%')
                    fs.writeFileSync(filedir, data, function(err){
                      if(err) console.log('写文件操作失败')
                      else console.log('写文件操作成功: ', filename)
                    })
                  }
                })
              }
            }
            if (isDir && deep < MAX_DEEP) {
              fileDisplay (filedir, deep + 1)
            }

          }
        })
      })
    }
  })
}

大佬有没有其它办法呀 (゜ー゜)如果不行的话, 以后只能一直采取先编译一个轻量分支, 再热切换回开发分支这种办法了

@yuche
Copy link
Contributor

yuche commented Apr 4, 2019

还可以试试用 chakra 来跑:
https://github.com/nodejs/node-chakracore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants