Node.js heap out of memory
Asked Answered
N

36

563

Today I ran my script for filesystem indexing to refresh RAID files index and after 4h it crashed with following error:

[md5:]  241613/241627 97.5%  
[md5:]  241614/241627 97.5%  
[md5:]  241625/241627 98.1%
Creating missing list... (79570 files missing)
Creating new files list... (241627 new files)

<--- Last few GCs --->

11629672 ms: Mark-sweep 1174.6 (1426.5) -> 1172.4 (1418.3) MB, 659.9 / 0 ms [allocation failure] [GC in old space requested].
11630371 ms: Mark-sweep 1172.4 (1418.3) -> 1172.4 (1411.3) MB, 698.9 / 0 ms [allocation failure] [GC in old space requested].
11631105 ms: Mark-sweep 1172.4 (1411.3) -> 1172.4 (1389.3) MB, 733.5 / 0 ms [last resort gc].
11631778 ms: Mark-sweep 1172.4 (1389.3) -> 1172.4 (1368.3) MB, 673.6 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x3d1d329c9e59 <JS Object>
1: SparseJoinWithSeparatorJS(aka SparseJoinWithSeparatorJS) [native array.js:~84] [pc=0x3629ef689ad0] (this=0x3d1d32904189 <undefined>,w=0x2b690ce91071 <JS Array[241627]>,L=241627,M=0x3d1d329b4a11 <JS Function ConvertToString (SharedFunctionInfo 0x3d1d3294ef79)>,N=0x7c953bf4d49 <String[4]\: ,\n  >)
2: Join(aka Join) [native array.js:143] [pc=0x3629ef616696] (this=0x3d1d32904189 <undefin...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/usr/bin/node]
 2: 0xe2c5fc [/usr/bin/node]
 3: v8::Utils::ReportApiFailure(char const*, char const*) [/usr/bin/node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/bin/node]
 5: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/usr/bin/node]
 6: v8::internal::Runtime_SparseJoinWithSeparator(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/bin/node]
 7: 0x3629ef50961b

Server is equipped with 16gb RAM and 24gb SSD swap. I highly doubt my script exceeded 36gb of memory. At least it shouldn't

Script creates index of files stored as Array of Objects with files metadata (modification dates, permissions, etc, no big data)

Here's full script code: http://pastebin.com/mjaD76c3

I've already experiend weird node issues in the past with this script what forced me eg. split index into multiple files as node was glitching when working on such big files as String. Is there any way to improve nodejs memory management with huge datasets?

Nastassia answered 25/7, 2016 at 2:45 Comment(3)
for windows cmd: set NODE_OPTIONS=--max-old-space-size=8192Catchpole
Can anyone confirm if this issue can occur due to less CPU. In my case I have 32 GB of RAM and specified about 11G for node options, but have only 2 CPU. Still getting OOM.Workman
I added to tsconfig.json to compilerOptions, "memoryLimit": 4096 This worked for meTonyatonye
A
553

If I remember correctly, there is a strict standard limit for the memory usage in V8 of around 1.7 GB, if you do not increase it manually.

In one of our products we followed this solution in our deploy script:

 node --max-old-space-size=4096 yourFile.js

There would also be a new space command but as I read here: a-tour-of-v8-garbage-collection the new space only collects the newly created short-term data and the old space contains all referenced data structures which should be in your case the best option.

Asyndeton answered 25/7, 2016 at 5:29 Comment(10)
In the same way this config is for nodejs independently on the framework.@SimerAsyndeton
I am developing with angular 4 and getting same issue, what should be yourFile.js file for angular app ?Perversity
@VikramSingh are you using ng serve or do you distribute the result of ng build the /dist folder by another webserver like express? But if your Angular project is using more than the standard 1.7GB Memory than you'll maybe have an architectural problem in your application? It looks like that you are using the development env with nmp start maybe this is a solution for it github.com/mgechev/angular-seed/issues/2063Asyndeton
I am using ng build with angular cli and aot ( dist folder )Perversity
which file put put in yourFile.js ?? Nodemon file or NodeJS file ?Corollary
index,js file @Corollary which you use to start serverAugmentative
node --max-old-space-size=1024 index.js #increase to 1gb node --max-old-space-size=2048 index.js #increase to 2gb node --max-old-space-size=3072 index.js #increase to 3gb node --max-old-space-size=4096 index.js #increase to 4gb node --max-old-space-size=5120 index.js #increase to 5gb node --max-old-space-size=6144 index.js #increase to 6gb node --max-old-space-size=7168 index.js #increase to 7gb node --max-old-space-size=8192 index.js #increase to 8gbFurred
If the problem still persists the only thing to do is buy more RAM, large scale project usually consume a lot of heap memoryMagnetostriction
@MekelIlyasa you can also allocate a large file as a swapfile, or, allocate a partition as a swap partition. It's not as fast as RAM but you don't necessarily need to buy more ram (not an option for people on laptops). Creating a swapfile is an immediate short term solution.Clarkin
any idea how to do it in npm. For example - npm --max-old-space-size=4096 run build will work?Printing
D
262

If you want to increase the memory usage of the node globally - not only single script, you can export environment variable, like this:
export NODE_OPTIONS=--max_old_space_size=4096

Then you do not need to play with files when running builds like npm run build.

Disconnect answered 23/11, 2018 at 8:56 Comment(5)
As an added convenience, add to bash_profile or zsh profile.Convenance
I'm getting the same error even after setting NODE_OPTIONS to 4096 or more. When I run the command npm run build, I see some processes running with commands like usr/bin/node --max_old_space_size=2048. What could be the reason?Richman
@AyeshWeerasinghe probably libraries or scripts that you are using or running have a hardcoded max_old_space_size parameter which overrides the exported env variable.Disconnect
Needed to wrap the value with double quotes like export NODE_OPTIONS="--max-old-space-size=4096"Afraid
Wondering whether it is --max_old_space_size or --max-old-space-size? > All options, including V8 options, allow words to be separated by both dashes (-) or underscores (_). For example, --pending-deprecation is equivalent to --pending_deprecation. nodejs.org/docs/latest-v18.x/api/cli.html#optionsUnderproduction
V
133

Just in case anyone runs into this in an environment where they cannot set node properties directly (in my case a build tool):

NODE_OPTIONS="--max-old-space-size=4096" node ...

You can set the node options using an environment variable if you cannot pass them on the command line.

Virology answered 18/9, 2018 at 15:55 Comment(4)
Can you please explain what you mean, when you say " ...set the node options using an environment variable.. "?Housekeeper
@Housekeeper An environment variable is a variable that has been set on the server that all processes can read the data from. Open an SSH terminal to your server and type: MY_VAR=hello then type: echo $MY_VAR. You will see that it prints "hello" in the terminal. You've just set an environment variable and read it back.Chateau
worked on Ubuntu 18.04, just added the export command to my bashrc filePopulous
Why does this answer use --max-old-space-size whereas other answers use --max_old_space_size?Precede
S
105

Here are some flag values to add some additional info on how to allow more memory when you start up your node server.

1GB - 8GB

#increase to 1gb
node --max-old-space-size=1024 index.js

#increase to 2gb
node --max-old-space-size=2048 index.js 

#increase to 3gb
node --max-old-space-size=3072 index.js

#increase to 4gb
node --max-old-space-size=4096 index.js

#increase to 5gb
node --max-old-space-size=5120 index.js

#increase to 6gb
node --max-old-space-size=6144 index.js

#increase to 7gb
node --max-old-space-size=7168 index.js

#increase to 8gb 
node --max-old-space-size=8192 index.js 

Stoichiometry answered 13/2, 2019 at 17:12 Comment(4)
can one keep increasing it in powers of 2? should one set it larger than system memory? if not what's a good system memory to max-old-space-size ratio?Shrine
@HarryMoreno You can actually put in any number value you like. Doesn't have to be in power of 2. Not sure about the ratio though. It's only a max limit, it wont be using all the memory. I would just set it as high as you need then scale back if needed.Stoichiometry
I'll give system ram - 1gb a try. Assuming this vm is only for running this node app.Shrine
@HarryMoreno A good system memory to max-old-space-size ratio depends entirely on what else is running on your machine. You can increase it in powers of two - or you can use any number. You can set it larger than system memory - but you will hit swap issues.Thaumaturge
S
74

I just faced same problem with my EC2 instance t2.micro which has 1 GB memory.

I resolved the problem by creating swap file using this url and set following environment variable.

export NODE_OPTIONS=--max_old_space_size=4096

Finally the problem has gone.

Sikko answered 17/4, 2020 at 2:57 Comment(3)
Thanks to share. I had the same problem, and your tip works fine for me.Psilocybin
Thanks. I'm now able to "yarn build" Strapi on my $5/mo Linode nanode instance after I created a 2GB swap file and added an "ENV NODE_OPTIONS=--max_old_space_size=1024" to my Dockerfile. Not sure the swap step was needed in my case but it can't hurt.Fauna
exactly, with small machines you may need to make it smaller rather than bigger, e.g. export NODE_OPTIONS=--max_old_space_size=1024Foretopmast
P
33

i was struggling with this even after setting --max-old-space-size.

Then i realised need to put options --max-old-space-size before the karma script.

also best to specify both syntaxes --max-old-space-size and --max_old_space_size my script for karma :

node --max-old-space-size=8192 --optimize-for-size --max-executable-size=8192  --max_old_space_size=8192 --optimize_for_size --max_executable_size=8192 node_modules/karma/bin/karma start --single-run --max_new_space_size=8192   --prod --aot

reference https://github.com/angular/angular-cli/issues/1652

Paramagnet answered 22/6, 2017 at 8:43 Comment(4)
"best to specify both syntaxes --max-old-space-size and --max_old_space_size" -- you do not need to do this, they are synonyms. From nodejs.org/api/cli.html#cli_options: "All options, including V8 options, allow words to be separated by both dashes (-) or underscores (_)."Pneumato
max-executable-size was removed and ends in an error when it is used: github.com/nodejs/node/issues/13341Nuriel
I had to cut it to --max-old-space-size=8192 --optimize-for-size --max_old_space_size=8192 --optimize_for_size and it workedRouse
Thanks, you're an absolute life-saver. optimize-for-size has finally allowed my builds to succeed.Goodfellowship
V
29

I encountered this issue when trying to debug with VSCode, so just wanted to add this is how you can add the argument to your debug setup.

You can add it to the runtimeArgs property of your config in launch.json.

See example below.

{
"version": "0.2.0",
"configurations": [{
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceRoot}\\server.js"
    },
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Training Script",
        "program": "${workspaceRoot}\\training-script.js",
        "runtimeArgs": [
            "--max-old-space-size=4096"
        ]
    }
]}
Vision answered 20/6, 2017 at 1:26 Comment(3)
Would launch.json be the same a package.json?Ilion
No, launch.json is a configuration file specifically for running an application from VS Code.Riffe
Where is this the launch.json file for VS Code?Excreta
T
29

I had a similar issue while doing AOT angular build. Following commands helped me.

npm install -g increase-memory-limit
increase-memory-limit

Source: https://geeklearning.io/angular-aot-webpack-memory-trick/

Tenrec answered 31/7, 2018 at 11:52 Comment(4)
Cheers worked for me, it may be necessary to sudo npm -g install increase-memory-limit --unsafe-permSandpiper
4k is not enough. developer was keep on 4k as static. good solution from developer. Also when I explore the npm page, I couldnt saw the info about change the limit value. Actually, There is a solution but didnt worked.Spriggs
Now in 2021 there must be better alternatives as the lib is marked as deprecated: npmjs.com/package/increase-memory-limitCutlery
After running this i type npm run dev and it stopped there. it is not showing any progress not giving any error after this line > webpack-dev-server --config ./webpack.dev.config.js. It was playing statue so, project cannot be run.Utilitarianism
P
27

I just want to add that in some systems, even increasing the node memory limit with --max-old-space-size, it's not enough and there is an OS error like this:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

In this case, probably is because you reached the max mmap per process.

You can check the max_map_count by running

sysctl vm.max_map_count

and increas it by running

sysctl -w vm.max_map_count=655300

and fix it to not be reset after a reboot by adding this line

vm.max_map_count=655300

in /etc/sysctl.conf file.

Check here for more info.

A good method to analyse the error is by run the process with strace

strace node --max-old-space-size=128000 my_memory_consuming_process.js
Parnassus answered 26/1, 2020 at 23:55 Comment(2)
The strace tip works, though note that if you're using Docker with the Node Alpine base image, you will have to install strace yourself.Radiotelegram
Also note that strace produces a ton of output lines, which will obstruct your ability to see the regular log lines (unless you have some sort of filtering of the output). Any way to cut down on this noise to only show events relevant to std::bad_alloc errors?Radiotelegram
I
18

I've faced this same problem recently and came across to this thread but my problem was with React App. Below changes in the node start command solved my issues.

Syntax

node --max-old-space-size=<size> path-to/fileName.js

Example

node --max-old-space-size=16000 scripts/build.js

Why size is 16000 in max-old-space-size?

Basically, it varies depends on the allocated memory to that thread and your node settings.

How to verify and give right size?

This is basically stay in our engine v8. below code helps you to understand the Heap Size of your local node v8 engine.

const v8 = require('v8');
const totalHeapSize = v8.getHeapStatistics().total_available_size;
const totalHeapSizeGb = (totalHeapSize / 1024 / 1024 / 1024).toFixed(2);
console.log('totalHeapSizeGb: ', totalHeapSizeGb);
Injunction answered 13/8, 2019 at 15:24 Comment(1)
Hi Venkat, I am facing an issue where my react dev server keeps getting crashed, even if I make a small change in my code and I get this error "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory". I have increased my memory allocation using --max_old_space_size=4096 but still facing the same issue. I have mac 8Gb RAM and M1 chip. I am using craco btw. The result of running the above script is "totalHeapSizeGb: 2.04". Can you please help "scripts": { "start": "craco start --max_old_space_size=4096 start" }Rapine
C
16

Steps to fix this issue (In Windows) -

  1. Open command prompt and type %appdata% press enter
  2. Navigate to %appdata% > npm folder
  3. Open or Edit ng.cmd in your favorite editor
  4. Add --max_old_space_size=8192 to the IF and ELSE block

Your node.cmd file looks like this after the change:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe" "--max_old_space_size=8192" "%~dp0\node_modules\@angular\cli\bin\ng" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node "--max_old_space_size=8192" "%~dp0\node_modules\@angular\cli\bin\ng" %*
)
Chimene answered 19/10, 2018 at 5:30 Comment(0)
S
11

Recently, in one of my project ran into same problem. Tried couple of things which anyone can try as a debugging to identify the root cause:

  1. As everyone suggested , increase the memory limit in node by adding this command:

    {
       "scripts":{
          "server":"node --max-old-space-size={size-value} server/index.js"
       }
    }
    

Here size-value i have defined for my application was 1536 (as my kubernetes pod memory was 2 GB limit , request 1.5 GB)

So always define the size-value based on your frontend infrastructure/architecture limit (little lesser than limit)

One strict callout here in the above command, use --max-old-space-size after node command not after the filename server/index.js.

  1. If you have ngnix config file then check following things:

    • worker_connections: 16384 (for heavy frontend applications) [nginx default is 512 connections per worker, which is too low for modern applications]

    • use: epoll (efficient method) [nginx supports a variety of connection processing methods]

    • http: add following things to free your worker from getting busy in handling some unwanted task. (client_body_timeout , reset_timeout_connection , client_header_timeout,keepalive_timeout ,send_timeout).

  2. Remove all logging/tracking tools like APM , Kafka , UTM tracking, Prerender (SEO) etc middlewares or turn off.

  3. Now code level debugging: In your main server file , remove unwanted console.log which is just printing a message.

  4. Now check for every server route i.e app.get() , app.post() ... below scenarios:

  • data => if(data) res.send(data) // do you really need to wait for data or that api returns something in response which i have to wait for?? , If not then modify like this:
data => res.send(data) // this will not block your thread, apply everywhere where it's needed
  • else part: if there is no error coming then simply return res.send({}) , NO console.log here.

  • error part: some people define as error or err which creates confusion and mistakes. like this:

    `error => { next(err) } // here err is undefined`
    
     `err => {next(error) } // here error is undefined`
    
     `app.get(API , (re,res) =>{
         error => next(error) // here next is not defined
      })`
    
  • remove winston , elastic-epm-node other unused libraries using npx depcheck command.

  • In the axios service file , check the methods and logging properly or not like :

      if(successCB) console.log("success") successCB(response.data) // here it's wrong statement, because on success you are just logging and then `successCB` sending  outside the if block which return in failure case also.
    
  • Save yourself from using stringify , parse etc on accessive large dataset. (which i can see in your above shown logs too.

  1. Last but not least , for every time when your application crashes or pods restarted check the logs. In log specifically look for this section: Security context This will give you why , where and who is the culprit behind the crash.
Searles answered 2/4, 2021 at 5:2 Comment(0)
M
10

I will mention 2 types of solution.

My solution : In my case I add this to my environment variables :

export NODE_OPTIONS=--max_old_space_size=20480

But even if I restart my computer it still does not work. My project folder is in d:\ disk. So I remove my project to c:\ disk and it worked.

My team mate's solution : package.json configuration is worked also.

"start": "rimraf ./build && react-scripts --expose-gc --max_old_space_size=4096 start",
Miscegenation answered 10/8, 2021 at 13:9 Comment(1)
This helped fix an issue in github actions with the build running out of memory. "build": "react-scripts --expose-gc --max_old_space_size=4096 build",Gryphon
S
8

For other beginners like me, who didn't find any suitable solution for this error, check the node version installed (x32, x64, x86). I have a 64-bit CPU and I've installed x86 node version, which caused the CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory error.

Sinistrorse answered 22/9, 2020 at 22:33 Comment(0)
C
8

--max-old-space-size

Unix (Mac OS)

  1. Open a terminal and open our .zshrc file using nano like so (this will create one, if one doesn't exist):

    nano ~/.zshrc

  2. Update our NODE_OPTIONS environment variable by adding the following line into our currently open .zshrc file:

    export NODE_OPTIONS=--max-old-space-size=8192 # increase node memory limit

Please note that we can set the number of megabytes passed in to whatever we like, provided our system has enough memory (here we are passing in 8192 megabytes which is roughly 8 GB).

  1. Save and exit nano by pressing: ctrl + x, then y to agree and finally enter to save the changes.

  2. Close and reopen the terminal to make sure our changes have been recognised.

  3. We can print out the contents of our .zshrc file to see if our changes were saved like so: cat ~/.zshrc.

Linux (Ubuntu)

  1. Open a terminal and open the .bashrc file using nano like so:

    nano ~/.bashrc

The remaining steps are similar with the Mac steps from above, except we would most likely be using ~/.bashrc by default (as opposed to ~/.zshrc). So these values would need to be substituted!

Link to Nodejs Docs

Crutch answered 14/2, 2022 at 5:30 Comment(0)
W
6

if you want to change the memory globally for node (windows) go to advanced system settings -> environment variables -> new user variable

variable name = NODE_OPTIONS
variable value = --max-old-space-size=4096
Walsingham answered 23/9, 2019 at 14:38 Comment(1)
This helped me on Win10.Rutter
A
5

You can also change Window's environment variables with:

 $env:NODE_OPTIONS="--max-old-space-size=8192"
Abiogenesis answered 17/10, 2020 at 17:47 Comment(1)
Where should I place it? package.json script or js file?Rutter
O
4

Use the option --optimize-for-size. It's going to focus on using less ram.

Ordinal answered 2/5, 2021 at 1:15 Comment(2)
this saved me from getting 'Segmentation fault'. Thank you so much!Piperonal
I tried various values for --max-old-space-size, and nothing worked for me. This is the only thing that did the trick for our custom build process for a React app running on an Azure App ServiceGonidium
I
3

I had this error on AWS Elastic Beanstalk, upgrading instance type from t3.micro (Free tier) to t3.small fixed the error

Imbibe answered 9/12, 2021 at 9:31 Comment(0)
S
2

In my case, I upgraded node.js version to latest (version 12.8.0) and it worked like a charm.

Sorrel answered 12/6, 2020 at 8:6 Comment(3)
Hello Angela and welcome to SO! Could you maybe specify the exact version of Node.js you updated to for future readers? Thanks!Erleena
I upgraded to Latest LTS Version: 12.18.0Sorrel
Didn't think this would work but it actually did! So for anyone reading this and their Node version is old, try upgrading to the latest version ( I went from a 10 version to 14.8), likely it will fix this issue for you. Many thanksThackeray
T
2

For Angular, this is how I fixed

In Package.json, inside script tag add this

"scripts": {
  "build-prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod",
},

Now in terminal/cmd instead of using ng build --prod just use

npm run build-prod

If you want to use this configuration for build only just remove --prod from all the 3 places

Trigeminal answered 12/3, 2021 at 17:7 Comment(0)
H
1

Upgrade node to the latest version. I was on node 6.6 with this error and upgraded to 8.9.4 and the problem went away.

Hardness answered 12/1, 2018 at 7:1 Comment(0)
D
1

If any of the given answers are not working for you, check your installed node if it compatible (i.e 32bit or 64bit) to your system. Usually this type of error occurs because of incompatible node and OS versions and terminal/system will not tell you about that but will keep you giving out of memory error.

Dissimulate answered 4/6, 2021 at 14:1 Comment(1)
This worked for me. I had incompatible node and npm versions. ThanksParcenary
H
1

I experienced the same problem today. The problem for me was, I was trying to import lot of data to the database in my NextJS project.

So what I did is, I installed win-node-env package like this:

yarn add win-node-env

Because my development machine was Windows. I installed it locally than globally. You can install it globally also like this: yarn global add win-node-env

And then in the package.json file of my NextJS project, I added another startup script like this:

"dev_more_mem": "NODE_OPTIONS=\"--max-old-space-size=8192\" next dev"

Here, am passing the node option, ie. setting 8GB as the limit. So my package.json file somewhat looks like this:

{
  "name": "my_project_name_here",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "dev_more_mem": "NODE_OPTIONS=\"--max-old-space-size=8192\" next dev",
    "build": "next build",
    "lint": "next lint"
  },
  ......
}

And then I run it like this:

yarn dev_more_mem

For me, I was facing the issue only on my development machine (because I was doing the importing of large data). Hence this solution. Thought to share this as it might come in handy for others.

Haemophilia answered 8/3, 2022 at 21:44 Comment(2)
"--max_old_space_size" should be written with hyphen : "--max-old-space-size", no ?Aftersensation
@Aftersensation thanks for pointing out that typo. I have edited my answer. Cheers!Haemophilia
U
1

I had the same issue in a windows machine and I noticed that for some reason it didn't work in git bash, but it was working in power shell

Ununa answered 30/8, 2022 at 10:3 Comment(0)
A
0

While using nodejs apps that produce heavy logging, a colleague solved this issue by piping the standard output(s) to a file.

Alginate answered 4/4, 2018 at 8:39 Comment(0)
M
0

If you are trying to launch not node itself, but some other soft, for example webpack you can use the environment variable and cross-env package:

$ cross-env NODE_OPTIONS='--max-old-space-size=4096' \
  webpack --progress --config build/webpack.config.dev.js
Myrilla answered 30/4, 2019 at 1:50 Comment(0)
R
0

For angular project bundling, I've added the below line to my pakage.json file in the scripts section.

"build-prod": "node --max_old_space_size=5120 ./node_modules/@angular/cli/bin/ng build --prod --base-href /"

Now, to bundle my code, I use npm run build-prod instead of ng build --requiredFlagsHere

Ranna answered 4/10, 2019 at 16:0 Comment(0)
R
0

My program was using two arrays. One that was parsed on JSON, the other that was generated from datas on the first one. Just before the second loop, I just had to set my first JSON parsed array back to [].

That way a lot of memory is freed, allowing the program to continue execution without failing memory allocation at some point.

Rattletrap answered 20/12, 2022 at 20:3 Comment(0)
C
0

I have fixed in Angular by making some changes in package.json file:

"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod --aot --build-optimizer",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
}

change your build to this it will solve the memory problem then run "npm run build" to build your project in production mode.

Chiou answered 24/5, 2023 at 23:48 Comment(0)
O
0

A workaround I used when encountering a heap out of memory error in React was to modify the "start" property in the react-scripts object within the package.json file to:

  "start": "node --max-old-space-size=4096 ./node_modules/react-scripts/scripts/start.js"
Olnee answered 1/2, 2024 at 5:58 Comment(0)
D
0

I also ran into this node-out-of-memory error.

Using NUXT3, I accidentally moved the node_modules directory into the pages directory.

So, NUXT tried to do its magic on each module file and node ran at some point into the limit.

Maybe my story is helpful to someone at some point :)

Diondione answered 14/3, 2024 at 15:36 Comment(0)
N
-1

In my case I had ran npm install on previous version of node, after some day I upgraded node version and ram npm install for few modules. After this I was getting this error. To fix this problem I deleted node_module folder from each project and ran npm install again.

Note : This was happening on my local machine and it got fixed on local machine only.

Notwithstanding answered 2/4, 2019 at 7:16 Comment(0)
A
-1

If you have limited memory or RAM, then go for the following command.

ng serve --source-map=false

It will be able to launch the application. For my example, it needs 16gb RAM. But I can run with 8gb RAM.

Aldaaldan answered 8/6, 2021 at 16:18 Comment(0)
C
-1

You can fix a "heap out of memory" error in Node.js by below approaches.

  1. Increase the amount of memory allocated to the Node.js process by using the --max-old-space-size flag when starting the application. For example, you can increase the limit to 4GB by running node --max-old-space-size=4096 index.js.

  2. Use a memory leak detection tool, such as the Node.js heap dump module, to identify and fix memory leaks in your application. You can also use the node inspector and use chrome://inspect to check memory usage.

  3. Optimize your code to reduce the amount of memory needed. This might involve reducing the size of data structures, reusing objects instead of creating new ones, or using more efficient algorithms.

  4. Use a garbage collector (GC) algorithm to manage memory automatically. Node.js uses the V8 engine's garbage collector by default, but you can also use other GC algorithms such as the Garbage Collection in Node.js

  5. Use a containerization technology like Docker which limits the amount of memory available to the container.

  6. Use a process manager like pm2 which allows to automatically restart the node application if it goes out of memory.

Coonskin answered 12/1, 2023 at 16:0 Comment(1)
did you use any generative AI at all in the writing of this answer post?Proselytize
A
-2

Check that you did not install the 32-bit version of node on a 64-bit machine. If you are running node on a 64-bit or 32-bit machine then the nodejs folder should be located in Program Files and Program Files (x86) respectively.

Akel answered 19/7, 2021 at 0:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.