Gatsby v4 Build Fails when using >2GB Node.js Heap

Hi there!

We recently upgraded from Gatsby v3 to v4, and it has resulted in Node.js heap out of memory errors (as seen below) - Node.js seems to be capped at 2GB of RAM during the build process.

How much RAM does the build system have? I’m assuming it is not the same amount as our plan, because our plan only has 1GB…

We have tried the following (see references at the bottom of this post):

  1. Setting NODE_OPTIONS="--max-old-space-size=4096" at the start of our npm script in package.json
  2. Setting GATSBY_CPU_COUNT=1 in the npm script
  3. Setting GATSBY_DISABLE_CACHE_PERSISTENCE=1 in the npm script
  4. Setting GATSBY_EXPERIMENTAL_QUERY_CONCURRENCY=1 in the npm script

But none of these have changed the failure that can be seen below.

Dec 27 10:15:13 PM   $ NODE_ENV=production gatsby build
Dec 27 10:15:19 PM   success open and validate gatsby-configs, load plugins - 2.340s
success onPreInit - 0.005s
success delete worker cache from previous builds - 0.002s
success initialize cache - 0.187s
success copy gatsby files - 0.111s
success Compiling Gatsby Functions - 0.375s
success onPreBootstrap - 0.389s
success createSchemaCustomization - 0.009s
success Checking for changed pages - 0.001s
success source and transform nodes - 100.238s
info Writing GraphQL type definitions to
Dec 27 10:17:01 PM   /opt/render/project/src/packages/website/.cache/schema.gql
success building schema - 4.794s
success createPages - 0.001s
success createPagesStatefully - 52.943s
info Total nodes: 1405, SitePage nodes: 120 (use --verbose for breakdown)
success Checking for changed pages - 0.000s
success onPreExtractQueries - 0.000s
success extract queries from components - 4.095s
success write out redirect data - 0.004s
success Build manifest and related icons - 0.058s
success onPostBootstrap - 0.065s
info bootstrap finished - 169.103s
success write out requires - 0.031s
success Building production JavaScript and CSS bundles - 144.090s
Dec 27 10:20:26 PM  
Dec 27 10:21:23 PM  
Dec 27 10:21:23 PM   <--- Last few GCs --->
Dec 27 10:21:23 PM  
Dec 27 10:21:23 PM   [1003:0x5e0c9c0]   368017 ms: Scavenge (reduce) 1857.0 (2041.4) -> 1857.0 (2041.4) MB, 2.3 / 0.0 ms  (average mu = 0.351, current mu = 0.335) external memory pressure
Dec 27 10:21:23 PM   [1003:0x5e0c9c0]   368731 ms: Mark-sweep (reduce) 1874.1 (2058.3) -> 1785.3 (2005.3) MB, 655.5 / 0.0 ms  (+ 5.2 ms in 1 steps since start of marking, biggest step 5.2 ms, walltime since start of marking 677 ms) (average mu = 0.330, current mu = 0.307) all
Dec 27 10:21:23 PM  
Dec 27 10:21:23 PM   <--- JS stacktrace --->
Dec 27 10:21:23 PM  
Dec 27 10:21:23 PM   FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Dec 27 10:21:23 PM    1: 0xb14d40 node::Abort() [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    2: 0xa31e30 node::FatalError(char const*, char const*) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    3: 0xcfb92e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    4: 0xcfbca7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    5: 0xee5f45  [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    6: 0xee6a8c  [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    7: 0xef49c1 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    8: 0xef7f1c v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM    9: 0xebbdc5 v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   10: 0xeb4934 v8::internal::FactoryBase<v8::internal::Factory>::AllocateRawWithImmortalMap(int, v8::internal::AllocationType, v8::internal::Map, v8::internal::AllocationAlignment) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   11: 0xeb6be1 v8::internal::FactoryBase<v8::internal::Factory>::NewRawTwoByteString(int, v8::internal::AllocationType) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   12: 0x112e4f5 v8::internal::String::SlowFlatten(v8::internal::Isolate*, v8::internal::Handle<v8::internal::ConsString>, v8::internal::AllocationType) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   13: 0xd072e9 v8::String::Utf8Length(v8::Isolate*) const [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   14: 0xaebba7  [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   15: 0xd627db  [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   16: 0xd63c7c  [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   17: 0xd642f6 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   18: 0x1611ad9  [/opt/render/project/nodes/node-16.2.0/bin/node]
Dec 27 10:21:23 PM   Aborted (core dumped)
Dec 27 10:21:23 PM   error Command failed with exit code 134.
Dec 27 10:21:23 PM   info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Dec 27 10:21:23 PM   error Command failed.
Dec 27 10:21:23 PM   Exit code: 134
Dec 27 10:21:23 PM   Command: /opt/render/project/nodes/node-16.2.0/bin/node
Dec 27 10:21:23 PM   Arguments: /opt/yarn-v1.22.5/lib/cli.js build
Dec 27 10:21:23 PM   Directory: /opt/render/project/src/packages/website
Dec 27 10:21:23 PM   Output:
Dec 27 10:21:23 PM  
Dec 27 10:21:23 PM   info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
Dec 27 10:21:23 PM   error Command failed with exit code 134.
Dec 27 10:21:23 PM   info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Dec 27 10:21:23 PM   error Command failed with exit code 134.
Dec 27 10:21:23 PM   info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Dec 27 10:21:23 PM   ==> Build failed 😞

References:

We just tried setting NODE_OPTIONS to --max-old-space-size=4096 as an environment variable in the Render dashboard, and this seems to make the build succeed.

But I wonder why this works and setting it in the npm script causes it to fail… :thinking:

Hi there,

I am glad you were able to get the build to succeed, regarding the NODE_OPTIONS, I believe you would need to set those as an explicit environment variable otherwise you can add the flag as part of your start command i.e. node --max-old-space-size=4096 app.js