开发项目有一段时间了,随着项目越来越大,打包的时间也相应的变长了,打包时的内存也增多了。这时候产生了一个问题,在发布项目的时候,会出现类似如下错误的提示。‘
<--- js stacktrace ---> ==== js stack trace ========================================= 0: exitframe [pc: 3295209e] 1: stubframe [pc: 32946989] security context: 0x0f312701 <jsobject> 2: replace [0f30b631](this=0x3ec73a65 <string[263]: d:/pdfund2/trunk/src/pdfundvue/node_modules/babel-loader/lib/ index.js!d:/pdfund2/trunk/src/pdfundvue/node_modules/vue-loader/lib/selector.js?type=script&index=0!d:/pdfund2/trunk/ src/pdfundvue/src/components/page/assetnetsharemssdqry/assetnetsharemssdqryselect.vue>,0x1334c6a5 <jsreg... fatal error: ineffective mark-compacts near heap limit allocation failed - javascript heap out of memory 1: 00f4254e node::makecallback+3774 2: 0159c652 v8::internal::heap::maxheapgrowingfactor+9554 3: 015933f1 v8::internal::scavengejob::operator=+16593 4: 01591f78 v8::internal::scavengejob::operator=+11352 5: 013fc644 v8::internal::stackguard::handleinterrupts+100 npm err! code elifecycle npm err! errno 134 npm err! pdfundvue@1.0.0 dev: `webpack-dev-server --inline --progress --watch --config build/webpack.dev.conf.js` npm err! exit status 134 npm err! npm err! failed at the pdfundvue@1.0.0 dev script. npm err! this is probably not a problem with npm. there is likely additional logging output above. npm err! a complete log of this run can be found in: npm err! d:\users\xxx\xx\xx\npm-cache\_logs\2019-07-03t05_43_41_196z-debug.log
原因不难知道,是因为在node中通过javascript使用内存时只能使用部分内存(64位系统:1.4 gb,32位系统:0.7 gb),这个时候,如果前端项目非常的庞大,webpack编译时就会占用很多的系统资源,如果超出了v8引擎对node默认的内存限制大小时,就会产生内存溢出的错误。
针对于不同的vue-cli 版本有不同的解决方法
vue-cli2遇到此问题的解决办法:
npm run dev 和 npm run build 直接在前面加上--max_old_space_size=4096
在 package.json 中,补充以下参数
"scripts": { "start": "npm run dev", "dev": "node --max_old_space_size=4096 build/dev-server.js", "build": "node --max_old_space_size=4096 build/build.js", "lint": "eslint --ext .js,.vue src", "pre": "node build/pre.js", "dll": "rimraf dist && webpack --progress --colors --config build/webpack.dll.conf.js" },
vue-cli3遇到此问题的解决办法:
方法一(已测试ok)
在 package.json 中,补充一下参数
{ "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", #加上这段代码 "fix-memory-limit": "cross-env limit=4096 increase-memory-limit" }, # 同时安装 2 个依赖包 "devdependencies": { "increase-memory-limit": "^1.0.3", "cross-env": "^5.0.5", } }
(1) .执行npm install 来安装2 个依赖包
(2).安装完成后,先执行一次 "fix-memory-limit"
如果安装失败则需要先删除掉项目的node_modules包
配置并安装成功后执行npm run fix-memory-limit这句(3).再执行"serve",就会出现报错了不是内部或外部命令,也不是可运行的程序或之类的错误
(4).将下边文件中"%_prog%"去掉双引号为 %_prog%(关键点)
(5)接口"serve" 启动即可
问题解决,可以正常运行
方法二(未尝试)
v8引擎对内存的使用的默认大小限制是1.4g,可以通过node.js命令设置限制来解决这个问题。修改package.json文件中内容,具体如下。
{ "scripts": { "serve": "npx --max_old_space_size=6144 vue-cli-service serve", "build": "npx --max_old_space_size=6144 vue-cli-service build --modern" }, }
修改完成后重启项目
到此这篇关于vue热更新出现内存溢出的解决方法的文章就介绍到这了,更多相关vue热更新内存溢出内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论