简体   繁体   中英

node --max_old_space_size is not working

Node Version: 6.9.x

My application was giving me FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory error.

So I tried changing the memory allocation using max_old_space_size flag

While executing my server.js, I am giving the --max_old_space_size=4096 argument.

However, it keeps crashing with same error as before. Also, I noticed the numbers in the error thrown while crashing. Looks like its still the default allocation of 1.4 GB.

Here's my error message:

<--- Last few GCs --->

   84567 ms: Mark-sweep 1375.1 (1401.9) -> 1374.7 (1402.9) MB, 88.7 / 0.4 ms (+ 0.8 ms in 3 steps since start of marking, biggest step 0.5 ms) [allocation failure] [GC in old space requested].
   84648 ms: Mark-sweep 1374.7 (1402.9) -> 1374.7 (1402.9) MB, 81.3 / 0.0 ms [allocation failure] [GC in old space requested].
   84734 ms: Mark-sweep 1374.7 (1402.9) -> 1374.3 (1401.9) MB, 86.0 / 0.0 ms [last resort gc].
   84825 ms: Mark-sweep 1374.3 (1401.9) -> 1374.0 (1400.9) MB, 90.9 / 0.0 ms [last resort gc].


<--- JS stacktrace --->

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

Security context: 0x1bf641bcfb39 <JS Object>
    1: slowToString [buffer.js:460] [pc=0x2f7049a5d3d5] (this=0x93e46a2d6b1 <an Uint8Array with map 0x13be78e068d9>,encoding=0x1bf641bdd309 <String[4]: utf8>,start=53,end=3522765)
    2: toString [buffer.js:~488] [pc=0x2f70499b77a6] (this=0x93e46a2d6b1 <an Uint8Array with map 0x13be78e068d9>)
    3: arguments adaptor frame: 3->0
    4: deserialize [/opt/myServer/node_modu...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [node]
 2: 0x10d2fbc [node]
 3: v8::Utils::ReportApiFailure(char const*, char const*) [node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
 5: v8::internal::Factory::NewRawOneByteString(int, v8::internal::PretenureFlag) [node]
 6: v8::internal::Factory::NewStringFromOneByte(v8::internal::Vector<unsigned char const>, v8::internal::PretenureFlag) [node]
 7: v8::internal::Factory::NewStringFromUtf8(v8::internal::Vector<char const>, v8::internal::PretenureFlag) [node]
 8: v8::String::NewFromUtf8(v8::Isolate*, char const*, v8::String::NewStringType, int) [node]
 9: node::StringBytes::Encode(v8::Isolate*, char const*, unsigned long, node::encoding) [node]
10: node::Buffer::Utf8Slice(v8::FunctionCallbackInfo<v8::Value> const&) [node]
11: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [node]
12: 0x9da914 [node]
13: 0x9daffe [node]
14: 0x2f7047a092a7
Aborted (core dumped)

In the "Last few GCs" section, the memory size mentioned is always around 1404 MB. What am I doing wrong?

Is the system not able to allocated it any more memory?

Make sure you put the options before your script's filename:

  • Correct : node --max-old-space-size=10000 index.js
  • Incorrect : node index.js --max-old-space-size=10000

This one got me for a while.

Now this flag is useless:

Versions of Node that are >= 12 should not need to use the --optimize_for_size and --max_old_space_size flags because JavaScript heap limit will be based on available memory.

If you use newer version of NodeJs, you may not need this while it should use all available RAM. Yet people still hitting 2gb or similar memory limits.

So here is the solution:

You can add the flag for a single script but I found out that it doesn't always work. I usually use some CI tool that runs commands on the shell so for me it's ok to not have flag inside the package.json scripts section, it may be an option for you too. There are 2 more options. So all 3:

Option 1. Set the flag to the single node script:

node --max-old-space-size=4076 /path/to/script.js

Option 2. Set env variable for single-shell:

NODE_OPTIONS="--max-old-space-size=4076"
node /path/to/script.js

Option 3. Set env variable for this shell and all processes spawned by this shell:

export NODE_OPTIONS="--max-old-space-size=4076"
node /path/to/script.js

Option #3 helped me many times when 1st didn't worked :)

make sure you have been using 64-bit NodeJS. I have a similar problem and after all I found out my problem was related to NodeJS itself. the 32-bit version of NodeJS can only give you up to 2GB of space.

尝试使用 --max-old-space-size=4096,如果它不起作用,请查看您是否有空闲内存来分配 4gb。

if you are using express generator then in package.json update script

scripts": {

   "start" :"node --max-old-space-size=4076 ./bin/www"

 }

the solution in my case was related with the html. i was using Vue 2 and on my html template i used dynamic data on a if statement:

<div>
  {{variableOne
    ?variableOne
    :variableTwo}}
  </div> 

so my error was that it seems that you can not write a if statement in more that one line inside the brackets, so i fixed it just writing the code in one line:

<div>
  {{variableOne?variableOne:variableTwo}}
  </div> 

在运行 npm 命令之前设置export NODE_OPTIONS="--max-old-space-size=16384"将起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM