Question

App Platform Node.js Service Heap Out Of Memory

Posted July 27, 2021 490 views
Node.jsDigitalOcean App Platform

I’m running a Node.js API as a service and while historically it ran perfectly fine. But recently it keeps failing with this error:

[2021-07-27 23:31:06] 
[2021-07-27 23:31:06] INFO[0045] Taking snapshot of full filesystem...        
[2021-07-27 23:31:41] INFO[0080] Pushing layer <registry-uri-3> to cache now 
[2021-07-27 23:31:41] INFO[0080] Pushing image to <registry-uri-4> 
[2021-07-27 23:31:41] INFO[0081] COPY . .                                     
[2021-07-27 23:31:41] INFO[0081] Taking snapshot of files...                  
[2021-07-27 23:31:41] INFO[0081] ENV NODE_ENV production                      
[2021-07-27 23:31:41] INFO[0081] No files changed in this command, skipping snapshotting. 
[2021-07-27 23:31:41] INFO[0081] RUN npm run build                            
[2021-07-27 23:31:41] INFO[0081] cmd: /bin/sh                                 
[2021-07-27 23:31:41] INFO[0081] args: [-c npm run build]                     
[2021-07-27 23:31:41] INFO[0081] Running: [/bin/sh -c npm run build]          
[2021-07-27 23:31:42] 
[2021-07-27 23:31:42] > api@1.0.0 build /app
[2021-07-27 23:31:42] > tsc
[2021-07-27 23:31:42] 
[2021-07-27 23:32:19] INFO[0119] Pushed image to 1 destinations               
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] <--- Last few GCs --->
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] [213:0x55d1a5f5ee40]    46077 ms: Mark-sweep (reduce) 1019.1 (1025.9) -> 1018.2 (1027.2) MB, 659.1 / 0.0 ms  (average mu = 0.197, current mu = 0.064) allocation failure scavenge might not succeed
[2021-07-27 23:32:30] [213:0x55d1a5f5ee40]    46819 ms: Mark-sweep (reduce) 1019.2 (1026.2) -> 1018.3 (1027.2) MB, 703.1 / 0.0 ms  (average mu = 0.127, current mu = 0.053) allocation failure scavenge might not succeed
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] <--- JS stacktrace --->
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
[2021-07-27 23:32:30] error building image: error building stage: failed to execute command: waiting for process to exit: signal: aborted
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] command exited with code 1
[2021-07-27 23:32:34]  ! Build failed (exit code 1)

It works completely fine locally.

Does anyone know what could be going on here?

This is the startup script:

node --max-old-space-size=4096 build/src/index.js

Dockerfile:

FROM node:14-alpine
WORKDIR /app

COPY ./package.json ./
RUN npm install

COPY . .

ENV NODE_ENV production
RUN npm run build

CMD ["npm", "run", "start"]

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hello @luisSnorkler,

It appears you are noticing the ‘JavaScript heap out of memory’ error while building your app.

You might need to add 2 environment variables:

1.  Key:  NODE_OPTIONS
    Value: --max-old-space-size=4096

2.  Key: GENERATE_SOURCEMAP
    Value: false

The second option usually resolves the issue. Upgrading to larger sizes wouldn’t be of much help in this case.

Here’s a documentation on the steps to add environment variables.

Cheers,
Taha Fatima

  • I was actually able to solve it by making my Docker file into a multi-step Docker file like this:

    FROM node:14-alpine as builder
    WORKDIR /app
    
    COPY ./package.json ./
    RUN npm install
    
    COPY . .
    
    ENV NODE_ENV production
    RUN npm run build
    
    FROM node:alpine as app
    WORKDIR /app
    
    COPY --from=builder /app/ ./
    
    CMD ["npm", "run", "start"]
    

From running diagnostics the memory used by the build process is 1.14 GB. But even when I upgraded to the larger sizes it still failed for the same reasons.

These are the diagnostics

Files:               593
Lines:            608779
Nodes:           1855634
Identifiers:      683426
Symbols:          951989
Types:            457061
Instantiations:  1912136
Memory used:    1145909K
I/O read:          0.07s
I/O write:         0.03s
Parse time:        3.57s
Bind time:         1.12s
Check time:       12.46s
Emit time:         2.49s
Total time:       19.64s