当前位置:   article > 正文

基于React的微前端项目构建时Node V8内存溢出问题解决(Ineffective mark-compacts near heap limit Allocation failed)_react内存溢出

react内存溢出

项目场景:

最近在写前端的React项目,项目用的是阿里飞冰的icestark微前端解决方案,面向大型系统的微前端解决方案,前端的每个模块都拆分成了微模块。编译好后在集成到主应用中。


问题描述 

由于单个微模块也是挺大的,热更新时随便改动下就经常报NodeJS的V8引擎内存堆栈溢出问题。

  1. <--- Last few GCs --->
  2. [7210:0x7fe5fba00000] 325240 ms: Mark-sweep (reduce) 4036.4 (4117.0) -> 4036.1 (4108.8) MB, 1394.5 / 0.1 ms (average mu = 0.111, current mu = 0.063) allocation failure scavenge might not succeed
  3. [7210:0x7fe5fba00000] 325244 ms: Scavenge (reduce) 4037.1 (4108.8) -> 4037.2 (4109.8) MB, 1.9 / 0.0 ms (average mu = 0.111, current mu = 0.063) allocation failure
  4. <--- JS stacktrace --->
  5. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
  6. 1: 0x10d0282b5 node::Abort() [/usr/local/bin/node]
  7. 2: 0x10d028438 node::OnFatalError(char const*, char const*) [/usr/local/bin/node]
  8. 3: 0x10d19fbb7 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
  9. 4: 0x10d19fb53 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
  10. 5: 0x10d33e1d5 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
  11. 6: 0x10d3421fb v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/usr/local/bin/node]
  12. 7: 0x10d33eadc v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/bin/node]
  13. 8: 0x10d33bf8a v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/bin/node]
  14. 9: 0x10d3492e0 v8::internal::Heap::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/usr/local/bin/node]
  15. 10: 0x10d349361 v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/usr/local/bin/node]
  16. 11: 0x10d3105dd v8::internal::FactoryBase<v8::internal::Factory>::NewRawOneByteString(int, v8::internal::AllocationType) [/usr/local/bin/node]
  17. 12: 0x10d710ec1 v8::internal::IncrementalStringBuilder::Extend() [/usr/local/bin/node]
  18. 13: 0x10d453244 v8::internal::JsonStringifier::SerializeString(v8::internal::Handle<v8::internal::String>) [/usr/local/bin/node]
  19. 14: 0x10d455058 v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_<false>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/usr/local/bin/node]
  20. 15: 0x10d458ca4 v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_<true>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/usr/local/bin/node]
  21. 16: 0x10d456307 v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_<false>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/usr/local/bin/node]
  22. 17: 0x10d4502b3 v8::internal::JsonStringify(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>) [/usr/local/bin/node]
  23. 18: 0x10d226838 v8::internal::Builtin_JsonStringify(int, unsigned long*, v8::internal::Isolate*) [/usr/local/bin/node]
  24. 19: 0x10da64379 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit [/usr/local/bin/node]
  25. 20: 0x1128f63f0
  26. 21: 0x10da7728f Builtins_ArrayForEach [/usr/local/bin/node]

 

 


 

原因分析:

Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 

JavaScript堆内存不足,Node 是基于V8引擎,在一般的后端开发语言中,在基本的内存使用上没有什么限制,但是在 Node 中通过 JavaScript 使用内存时只能使用部分内存(64位系统下约为1.4 GB,32位系统下约为0.7 GB),这就是编译项目时为什么会出现内存泄露了,因为前端项目如果非常的庞大,webpack 编译时就会占用很多的系统资源,如果超出了V8对 Node 默认的内存限制大小就会报这个错误。
 


解决方案:

由于使用的微前端的架构,构建工具也是使用阿里飞冰的build-scripts模块,所以使用在网上查到的vue项目的内存溢出配置也是无效,比如这样也是错误的

【build-scripts -max_old_space_size=8000 start】

只有改成下面的方式才有效。

node --max_old_space_size=8000 node_modules/.bin/build-scripts start

node --max_old_space_size=8000 node_modules/.bin/build-scripts start

  

最后附完整的package.json文件

  1. {
  2. "name": "@qc/qc-module",
  3. "version": "1.0.0",
  4. "description": "QC班长的微模块",
  5. "files": [
  6. "demo/",
  7. "es/",
  8. "lib/",
  9. "build/"
  10. ],
  11. "main": "lib/index.js",
  12. "module": "es/index.js",
  13. "stylePath": "style.js",
  14. "scripts": {
  15. "start": "node --max_old_space_size=8000 node_modules/.bin/build-scripts start",
  16. "build": "build-scripts build",
  17. "test": "build-scripts test",
  18. "prepublishOnly": "npm run build",
  19. "eslint": "eslint --cache --ext .js,.jsx,.ts,.tsx ./",
  20. "eslint:fix": "npm run eslint -- --fix",
  21. "stylelint": "stylelint \"**/*.{css,scss,less}\"",
  22. "lint": "npm run eslint && npm run stylelint"
  23. },
  24. "keywords": [
  25. "demo",
  26. "react",
  27. "component"
  28. ],
  29. "dependencies": {
  30. "@antv/g2plot": "^2.4.28",
  31. "@ice/stark-module": "^1.5.0",
  32. "@react-spring/web": "~9.5.5",
  33. "ahooks": "^3.1.14",
  34. "prop-types": "^15.5.8",
  35. "react-to-print": "^2.14.12",
  36. "umi-request": "^1.4.0"
  37. },
  38. "devDependencies": {
  39. "@ant-design/icons": "^5.0.1",
  40. "@iceworks/spec": "^1.0.0",
  41. "@types/react": "^17.0.2",
  42. "@types/react-dom": "^17.0.2",
  43. "build-plugin-component": "^1.0.0",
  44. "build-plugin-stark-module": "^2.0.0",
  45. "build-scripts": "^1.1.1",
  46. "copy-webpack-plugin": "^5.1.2",
  47. "enzyme": "^3.10.0",
  48. "react": "^17.0.2",
  49. "react-dom": "^17.0.2",
  50. "xlsx": "^0.18.5"
  51. },
  52. "peerDependencies": {
  53. "react": "^16 || ^17"
  54. },
  55. "componentConfig": {
  56. "name": "FormModuel",
  57. "title": "form-moduel",
  58. "category": "Form"
  59. },
  60. "publishConfig": {
  61. "access": "public"
  62. },
  63. "license": "MIT",
  64. "homepage": "https://unpkg.com/@yth/test-moduel@1.0.0/build/index.html"
  65. }

 参考文献

1、GitHub - ice-lab/build-scripts: 基于 Webpack 的插件化工程构建工具,支持快速建设一套开箱即用的工程方案。

2、icestark-面向大型系统的微前端解决方案 | icestark

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/651727
推荐阅读
相关标签
  

闽ICP备14008679号