4

I'm facing with this issue of "JavaScript heap out of memory" when I deploy or ru service with 'serverless offiline' command.

I'm using nestjs - a node framework - and building the project for node 10x. On my terminal I got this below.

I found some fixes like

  1. type " node --max-old-space-size=1024 index.js" on terminal
  2. use this package https://www.npmjs.com/package/increase-memory-limit
  3. append on script tag in package.json some like ""scripts": { "webpacker": "node --max_old_space_size=4096" not of theses works.

any clue?

PS D:\m1_workspace\dw-api> serverless offline
Serverless: Compiling with Typescript...
Serverless: Using local tsconfig.json

<--- Last few GCs --->
al[21864:000001EF81231660]    20688 ms: Mark-sweep 1394.2 (1429.4) -> 1392.3 (1429.9) MB, 977.1 / 0.0 ms  (+ 0.0 ms in 62 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 987 ms) (average mu = 0.074, current mu = 0.010) all[21864:000001EF81231660]    21557 ms: Mark-sweep 1392.3 (1429.9) -> 1392.2 (1427.9) MB, 868.1 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 868 ms) (average mu = 0.037, current mu = 0.001) allo

<--- JS stacktrace --->

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

Security context: 0x002e2c61e6e9 <JSObject>
    0: builtin exit frame: splice(this=0x03a8c4a97e89 <JSArray[8]>,0x0237e40868f9 <TypeObject map = 000001453BA516C9>,0,8,0x03a8c4a97e89 <JSArray[8]>)

    1: getUnionType(aka getUnionType) [00000057B5C33821] [D:\m1_workspace\dw-api\node_modules\@hewmen\serverless-plugin-typescript\node_modules\typescript\lib\typescript.js:~34245] [pc=000003F28C0363E9](this=0x007f886026f1 <undefined>,types=0x010...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1
  • What is your code doing when it runs out of memory? Possibly a bunch of asynchronous operations all in flight at the same time? Or reading large files into memory? Or parsing large pieces of JSON? Commented Jul 2, 2020 at 4:06

3 Answers 3

8

A quick workaround is to try to run below command first:

export NODE_OPTIONS=--max_old_space_size=8192

I have a large serverless project which ran into similar issue when I tried to deploy with "sls deply". And this workaround works for me.

Hope it can help.

Sign up to request clarification or add additional context in comments.

2 Comments

while running, it shows, export is not recornized
If you are using this in a script then the following could be useful NODE_OPTIONS=--max_old_space_size=8192 npx sls package (you can set the environment variable for just one command instead of at the level of the shell)
3

This was happening to me too -
I realized I had defined my serverless configuration to package each lambda individually.
Which looks like this:

package:
  individually: true

Changing that to:

package:
  individually: false

worked for me.

(Of course if packaging your lambda functions individually is crucial for you, then you'll lose that, but for me it wasn't).

Comments

1

Check concurrency in serverless webpack

Reducing custom.webpack.concurrency to a lower number in my serverless.yml was a deal breaker. Start with 1.

1 Comment

100%, this is the better answer IMO. Yes, you can YOLO 8GB memory, and that worked for me too, but reducing concurrency seems the more efficient way to handle this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.