Hướng dẫn vite build javascript heap out of memory - vite xây dựng đống javascript hết bộ nhớ

Xin chào, Hãy để tôi kể cho bạn nghe một câu chuyện về tôi gặp phải vấn đề này hai lần trong 1 năm qua và may mắn là những trường hợp đó đã được giải quyết, tôi hy vọng điều này hữu ích.

Show

Cuộc gặp gỡ đầu tiên

Tôi đã quên chi tiết chính xác về cái đầu tiên vì đó là một thời gian dài trước đây và hàng trăm cam kết trước đây, vì vậy tôi rất khó để theo dõi và kiểm tra lại, nhưng đó cũng là vấn đề bộ nhớ. Nhưng vấn đề là nó không sử dụng Vite, nó chỉ đơn giản là CRA với Craco. Vì vậy, tôi nghĩ đó là một thông tin thú vị.

Tôi đã thử cách giải quyết --max-old-space-size=xxxx và đặt ngưỡng thành 8GB, đôi khi nó hoạt động đôi khi nó không, và nó đã xảy ra ở giữa sự di chuyển của chúng tôi từ vật liệu UI V4 sang V5, sau khi việc di chuyển hoàn thành cũng bao gồm việc làm sạch / thay đổi một số Sự phụ thuộc không hỗ trợ V5, bằng cách nào đó lỗi biến mất mãi mãi (hoặc tôi nghĩ vậy).

Cuộc gặp gỡ thứ hai

Nó đã xảy ra gần đây với cùng một dự án nhưng nó đã sử dụng VITE phiên bản 2, các bản ghi luôn dừng lại ở: ✓ xxxx modules transformed và sau đó ném lỗi JavaScript heap out of memory. Nhìn vào việc sử dụng bộ nhớ, nó luôn ổn định nhưng sau đó nhật ký ✓ xxxx modules transformed được hiển thị, Mem sử dụng tên lửa Sky như điên. Nó hoạt động tốt trên máy của tôi vì nó có RAM 32 GB, nhưng nó không thành công trên CI của chúng tôi.

--max-old-space-size=xxxx sẽ chỉ giúp nút để giữ MEM bên dưới xxxx nếu bạn may mắn sẽ hoạt động, nhưng nếu bạn không may mắn (như tôi), nó sẽ luôn yêu cầu sử dụng MEM trên ngưỡng đó trong trường hợp đó JavaScript heap out of memory xảy ra một lần nữa. Tôi đã cố gắng cập nhật lên Vite 3 nhưng lỗi tương tự vẫn xảy ra.

Và tất cả cuộc gặp gỡ thứ 2 này xảy ra sau khi một cam kết thêm một gói có tên là City.json. Hóa ra, gói đó có một tệp JSON tương đối lớn (tôi nhớ nó là khoảng 6MB). Và chúng tôi nhập nó trực tiếp vào ứng dụng React! 6MB ... Chúa ơi.

Vì vậy, tôi đoán, có lẽ điều này đang xảy ra ở giai đoạn thu nhỏ / ulifying / sourcemaping và với kích thước tệp đó, tôi nghĩ rằng nó có khả năng ăn nhiều bộ nhớ trong một lần. Vì vậy, tôi bắt đầu sao chép cities.json và tước dữ liệu, vì có một số dữ liệu mà chúng tôi không sử dụng và tôi có thể giảm kích thước tệp thành 1.6MB, nó vẫn lớn nhưng tốt hơn nhiều so với bản gốc.

Và tôi đã thay đổi nhập để sử dụng nhập động. Không phải vì mối quan tâm về kích thước tệp mà (tôi giả sử), bộ minifer sẽ có một nhiệm vụ dễ dàng hơn để phân tích riêng biệt 1.6MB (tôi giả sử gói tách có ảnh hưởng đến cách thức sử dụng minifier sử dụng MEM).

Và thử vận ​​may của tôi để đẩy nó và kiểm tra trên CI ... nó hoạt động.

Sự kết luận

Tôi trở nên tự tin rằng thủ phạm là trong giai đoạn thu nhỏ / xấu xí / sourcemaping không liên quan gì đến vite (hoặc là nó?). Tôi không quen thuộc với nội bộ VITE hoặc CRA nhưng tôi đọc cả hai đang sử dụng Terser cho Minifier, vì vậy có lẽ chúng ta nên nhìn vào Terser?

Đề xuất của tôi cho những người vẫn đang gặp phải vấn đề này, hãy kiểm tra các phụ thuộc của bạn và kích thước đầu ra của gói của bạn, cố gắng giảm nó nhiều nhất có thể và cố gắng vô hiệu hóa Sourcemap trên bản dựng CI như những người khác đã nói. Nếu vấn đề này đang xảy ra với bạn có lẽ đó là một chỉ số bạn đang làm điều gì đó sai trên cơ sở mã của mình, có lẽ nó quá lớn hoặc quá phức tạp.

Tôi biết rất ít để không có gì về tất cả những điều này, xin lỗi vì văn bản dài, và cảm ơn vì những công cụ tuyệt vời này! Hòa bình

Bạn đang sử dụng phiên bản $ npm run build-storybook > build-storybook > build-storybook info @storybook/react v6.5.8 info info => Cleaning outputDir: {portal}/storybook-static info => Loading presets info => Copying static files: {portal}/public at {portal}/storybook-static/ info => Compiling manager.. (node:10077) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at {portal}/node_modules/yaml/package.json. Update this package.json to use a subpath pattern like "./*". (Use `node --trace-deprecation ...` to show where the warning was created) vite v2.9.12 building for production... transforming (1050) node_modules/@mui/material/Checkbox/checkboxClasses.jsinfo => Manager built (43 s) transforming (1417) node_modules/html-entities/lib/surrogate-pairs.js[BABEL] Note: The code generator has deoptimised the styling of {portal}/src/api/client/gen/api.ts as it exceeds the max of 500KB. transforming (27756) node_modules/react-pdf/dist/esm/Ref.jsUse of eval is strongly discouraged, as it poses security risks and may cause issues with minification Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification ✓ 27757 modules transformed. Generated an empty chunk: "react-dom-client-placeholder" rendering chunks (46)... <--- Last few GCs ---> [10077:0x698f720] 155709 ms: Scavenge 3995.6 (4109.0) -> 3996.3 (4109.5) MB, 12.2 / 0.0 ms (average mu = 0.750, current mu = 0.432) allocation failure [10077:0x698f720] 155724 ms: Scavenge 3996.3 (4109.5) -> 3995.5 (4133.0) MB, 14.7 / 0.0 ms (average mu = 0.750, current mu = 0.432) allocation failure [10077:0x698f720] 157877 ms: Mark-sweep 4026.1 (4147.9) -> 4009.5 (4146.3) MB, 2138.8 / 0.1 ms (average mu = 0.510, current mu = 0.041) allocation failure scavenge might not succeed <--- JS stacktrace ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb09c10 node::Abort() [node] 2: 0xa1c193 node::FatalError(char const*, char const*) [node] 3: 0xcf8dde v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node] 4: 0xcf9157 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node] 5: 0xeb09f5 [node] 6: 0xec06bd v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node] 7: 0xec33be v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node] 8: 0xe848fa v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node] 9: 0x11fd646 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node] 10: 0x15f20b9 [node] Aborted 0 nào?

^2.9.12

Thông tin hệ thống và phiên bản sách truyện

Hệ thống: OS: Linux 5.10 Ubuntu 21.10 21.10 (Impish Indri) CPU: (24) X64 AMD Ryzen 9 3900X Bộ xử lý 12 core Binaries: Node: 16.15.1 - ~/.nvm/phiên bản/Nút/V16.15.1 Sợi nút: 1.22.17 - ~/.yarn/bin/sợi npm: 8.11.0 - ~/.nvm/phiên bản/nút/v16.15.1/bin/npm => 6.5.8 @Storybook/addon-Actions: ^6.5.8 => 6.5.8 @Storybook/addon-docs: ^6.5.8 => 6.5.8 @Storybook/addon-essentials: ^6.5.8 =>> 6.5.8 @Storybook/addon-links: ^6.5.8 => 6.5.8 @StoryBook/Builder-Vite: ^0.1.36 => 0.1.36 @StoryBook/Node-Logger: ^6.5.8 => 6.5. 8 @Storybook/preet-create-react-app: ^3.2.0 => 3.2.0 @Storybook/React: ^6.5.8 => 6.5.8
OS: Linux 5.10 Ubuntu 21.10 21.10 (Impish Indri)
CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
Binaries:
Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
Yarn: 1.22.17 - ~/.yarn/bin/yarn
npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
npmPackages:
@storybook/addon-a11y: ^6.5.8 => 6.5.8
@storybook/addon-actions: ^6.5.8 => 6.5.8
@storybook/addon-docs: ^6.5.8 => 6.5.8
@storybook/addon-essentials: ^6.5.8 => 6.5.8
@storybook/addon-links: ^6.5.8 => 6.5.8
@storybook/builder-vite: ^0.1.36 => 0.1.36
@storybook/node-logger: ^6.5.8 => 6.5.8
@storybook/preset-create-react-app: ^3.2.0 => 3.2.0
@storybook/react: ^6.5.8 => 6.5.8

"terser": "^5.13.0",
"@vitejs/plugin-react": "^1.1.3",
"vite-plugin-babel-macros": "^1.0.6",
"vite-plugin-checker": "^0.4.6",
"vite-plugin-environment": "^1.1.1",
"vite-plugin-html": "^3.2.0",
"vite-plugin-imp": "^2.2.0",
"vite-plugin-mdx": "^3.5.10",
"vite-plugin-svgr": "^2.1.0",
"vitest": "^0.14.2"

Mô tả lỗi

Gần đây, tôi đang chuyển đổi một dự án Crav4 sang Vite, tôi đã thử xây dựng Storybook và thấy nó thất bại với lỗi bộ nhớ. Tôi đã liên tục từ chối chuyển đổi VITE để tôi có so sánh CRAV4/WebPack4 của cùng một dự án.

Nó không phải là một vòng lặp vô hạn của một số bộ nhớ mệt mỏi. Nếu tôi xây dựng bằng cách sử dụng

$ npm run build-storybook

>  build-storybook
> build-storybook

info @storybook/react v6.5.8
info 
info => Cleaning outputDir: {portal}/storybook-static
info => Loading presets
info => Copying static files: {portal}/public at {portal}/storybook-static/
info => Compiling manager..
(node:10077) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at {portal}/node_modules/yaml/package.json.
Update this package.json to use a subpath pattern like "./*".
(Use `node --trace-deprecation ...` to show where the warning was created)
vite v2.9.12 building for production...
transforming (1050) node_modules/@mui/material/Checkbox/checkboxClasses.jsinfo => Manager built (43 s)
transforming (1417) node_modules/html-entities/lib/surrogate-pairs.js[BABEL] Note: The code generator has deoptimised the styling of {portal}/src/api/client/gen/api.ts as it exceeds the max of 500KB.
transforming (27756) node_modules/react-pdf/dist/esm/Ref.jsUse of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
✓ 27757 modules transformed.
Generated an empty chunk: "react-dom-client-placeholder"
rendering chunks (46)...
<--- Last few GCs --->

[10077:0x698f720]   155709 ms: Scavenge 3995.6 (4109.0) -> 3996.3 (4109.5) MB, 12.2 / 0.0 ms  (average mu = 0.750, current mu = 0.432) allocation failure 
[10077:0x698f720]   155724 ms: Scavenge 3996.3 (4109.5) -> 3995.5 (4133.0) MB, 14.7 / 0.0 ms  (average mu = 0.750, current mu = 0.432) allocation failure 
[10077:0x698f720]   157877 ms: Mark-sweep 4026.1 (4147.9) -> 4009.5 (4146.3) MB, 2138.8 / 0.1 ms  (average mu = 0.510, current mu = 0.041) allocation failure scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb09c10 node::Abort() [node]
 2: 0xa1c193 node::FatalError(char const*, char const*) [node]
 3: 0xcf8dde v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xcf9157 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xeb09f5  [node]
 6: 0xec06bd v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 7: 0xec33be v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
 8: 0xe848fa v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
 9: 0x11fd646 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
10: 0x15f20b9  [node]
Aborted
1 (8GB) thì nó sẽ xây dựng thành công. Tuy nhiên, việc xây dựng với mặc định hoặc 4GB là không đủ và xảy ra lỗi bộ nhớ.

Đây dường như không phải là một cuốn truyện hay vấn đề VITE nhưng cụ thể là một người xây dựng vite vì:

  • Trong cùng một dự án theo Crav4 và Storybook (cùng phiên bản) với WebPack 4, Storybook xây dựng thành công ở giới hạn bộ nhớ mặc định
  • Vite Build Xây dựng thành công ở mức 4GB, trong khi Vite+Storybook mất 8GB

$ npm run build-storybook

>  build-storybook
> build-storybook

info @storybook/react v6.5.8
info 
info => Cleaning outputDir: {portal}/storybook-static
info => Loading presets
info => Copying static files: {portal}/public at {portal}/storybook-static/
info => Compiling manager..
(node:10077) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at {portal}/node_modules/yaml/package.json.
Update this package.json to use a subpath pattern like "./*".
(Use `node --trace-deprecation ...` to show where the warning was created)
vite v2.9.12 building for production...
transforming (1050) node_modules/@mui/material/Checkbox/checkboxClasses.jsinfo => Manager built (43 s)
transforming (1417) node_modules/html-entities/lib/surrogate-pairs.js[BABEL] Note: The code generator has deoptimised the styling of {portal}/src/api/client/gen/api.ts as it exceeds the max of 500KB.
transforming (27756) node_modules/react-pdf/dist/esm/Ref.jsUse of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
✓ 27757 modules transformed.
Generated an empty chunk: "react-dom-client-placeholder"
rendering chunks (46)...
<--- Last few GCs --->

[10077:0x698f720]   155709 ms: Scavenge 3995.6 (4109.0) -> 3996.3 (4109.5) MB, 12.2 / 0.0 ms  (average mu = 0.750, current mu = 0.432) allocation failure 
[10077:0x698f720]   155724 ms: Scavenge 3996.3 (4109.5) -> 3995.5 (4133.0) MB, 14.7 / 0.0 ms  (average mu = 0.750, current mu = 0.432) allocation failure 
[10077:0x698f720]   157877 ms: Mark-sweep 4026.1 (4147.9) -> 4009.5 (4146.3) MB, 2138.8 / 0.1 ms  (average mu = 0.510, current mu = 0.041) allocation failure scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb09c10 node::Abort() [node]
 2: 0xa1c193 node::FatalError(char const*, char const*) [node]
 3: 0xcf8dde v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xcf9157 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xeb09f5  [node]
 6: 0xec06bd v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 7: 0xec33be v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
 8: 0xe848fa v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
 9: 0x11fd646 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
10: 0x15f20b9  [node]
Aborted

Liên kết đến ví dụ có thể tái tạo tối thiểu

Không phản hồi

Sự tham gia

  • Tôi sẵn sàng gửi một yêu cầu kéo cho vấn đề này.