#About RAM usage

1 messages · Page 1 of 1 (latest)

noble dagger
#

So I just tried Nest.js for about a week and I notice that the RAM usage to run it is quite high, about 2 GB.

While my pc can definitely run it, my ec2 instance with 1gb just freeze every time I tried to run it, building the project also cost me about 2 GB of RAM.

After searching for a bit, someone said it could be because I'm using Node v18, so I try installing the latest one (20.15.1) then delete the dist folder (someone mention about conflicting build) and then build and run, and it did actually reduced to about 100 MB.

The current problem is that my ec2 has already using the latest Node and the build process can't finish because it keeps running out of memory.

Is there anything I can try to solve this high RAM usage, or my only option is to upgrade?

covert coyote
#

Depends on the build process. You could try switching to SWC (https://docs.nestjs.com/recipes/swc), which is just a transpiler (converts ts to js, as opposed to tsc, which also performs type checks which could be memory intensive).

#
/usr/bin/time -l yarn swc --strip-leading-paths --copy-files -d dist src
Successfully compiled: 386 files, copied 10 files with swc (172.57ms)
        0,92 real         0,80 user         0,44 sys
         221 396 992  maximum resident set size
                   0  average shared memory size
                   0  average unshared data size
                   0  average unshared stack size
               22898  page reclaims
                  10  page faults
                   0  swaps
                   0  block input operations
                   0  block output operations
                   0  messages sent
                   0  messages received
                   0  signals received
                1023  voluntary context switches
               30504  involuntary context switches
          2694013355  instructions retired
           903384751  cycles elapsed
            82602432  peak memory footprint

An example on somewhat large project, swc used 221MB.

peak jolt
#

Are you running in prod mode? I once ran it with the following commands and it took 2 GB

npm run build
npm run start

But that's wrong, you need to use the following commands:

npm run build
npm run start:prod

Then it should use a lot less memory