Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless Framework - JavaScript heap out of memory

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
like image 474
Cleomar Pestilli Avatar asked Sep 12 '25 11:09

Cleomar Pestilli


2 Answers

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.

like image 54
Jacky Wu Avatar answered Sep 15 '25 01:09

Jacky Wu


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).

like image 39
gillyb Avatar answered Sep 15 '25 01:09

gillyb