JavaScript heap out of memory

JavaScript heap out of memory

JavaScript heap out of memory error solution

The official explanation is as follows:

What is the memory limit on a node process?
Currently, by default v8 has a memory limit of 512MB on 32-bit systems, and 1.4GB on 64-bit systems. The limit can be raised by setting --max_old_space_size to a maximum of ~1024 (~1 GB) (32-bit) and ~4096 (~4GB) (64-bit), but it is recommended that you split your single process into several workers if you are hitting memory limits.
 

By default, the official default configuration of the V8 engine used by nodejs is to pay attention to 1.4g of memory in a 64-bit system, which is just the same as the above prompt, and the maximum memory size is 4g. The official also recommends that if you use larger memory, you need to divide it into more This is how a small worker avoids the use of large memory.

So there are two solutions:

  • Used when executing the node command

    max_old_space_size=4096This parameter sets the maximum 4gbmemory

    For example, the previous one node index.jsbecomesnode --max_old_space_size=4096 index.js

  • A package mounted directly npm increase-memory-limitpacket address as follows:

    https://www.npmjs.com/package/increase-memory-limit

    This package will automatically increase as a command --max_old_space_size=4096parameter.

@vue/cli3.x version of JavaScript heap out of memory processing

1. Use the above operations to expand the memory configuration in the project

2. Modify the globally installed vue startup script to add -max_old_space_size=4096parameters

Here for the vuecli processing of the global installation of yarn, execute the following command in the cmd console:

notepad %USERPROFILE%/AppData/Local/Yarn/Data/global/node_modules/.bin/vue.cmd
 

Edit and open the contents of the file as follows:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe --max_old_space_size=4096"  "%~dp0\..\@vue\cli\bin\vue.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node --max_old_space_size=4096  "%~dp0\..\@vue\cli\bin\vue.js" %*
)
 

Secondly, for the vue-cli-server processing in the project, open the cmd console in the script project directory and execute the following commands:

notepad ./node_modules/.bin/vue-cli-server.cmd
 

Edit and open the contents of the file as follows:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe" --max_old_space_size=4096 "%~dp0\..\@vue\cli-service\bin\vue-cli-service.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node --max_old_space_size=4096  "%~dp0\..\@vue\cli-service\bin\vue-cli-service.js" %*
)