4

I'm currently trying to debug the memory consumption of my NestJs app and running into some issues. As is, my app consumes around ~920 MB of RAM right after starting the app. The consumption does not go up significantly which leads me to believe that it's not a memory leak causing the memory consumption.

To get started, I'm trying to get to a point where the excess memory consumption does not happen. For this I commented out all consumed modules from app/module.ts and removed the imports. I also commented out everything in main.ts but the memory consumption is still the same.

app.module

export const appModuleMetaData: ModuleMetadata = {
    imports: [
        ....
        // AuthModule,
        // ... -> I removed all modules
    ],
    providers: [],
    exports: [],
};

main.ts

async function bootstrap() {
    const app = await NestFactory.create(AppModule);

    // const configService = app.get(ConfigService);
    // .... -> I removed everything here

    await app.listen(8012);
}
bootstrap();

I'm trying to figure out what nest loads into memory on boot. My understanding would be that removing all the modules and clearing out main.ts should result in a memory state of a newly created project. When I create a sample app however, it consumes only ~90 MB of Ram (after builing and running the compliled code).

I'm running NestJs from a monorepo but not in monorepo mode (in case that's important).

I'd love any help as I'm stuck.

1
  • 1
    Did you ever find any answers to your question? I'm in a similar situation. Jul 31, 2022 at 1:19

2 Answers 2

8

I ran into memory pressure with a fairly standard implementation of a NestJs RESTful API. Deployed to a Google App Engine F1 sized instance (capped at 256mb memory), although I think AWS Lambda works similarly for the sake of this post.

The instance would be killed off because allocated memory was exceeded. In fact, it would fail to start most times.

After fumbling around, I found 2 concrete things that had an immediate impact:

Garbage collection

By default Node limits itself to use a certain amount of memory. Garbage collection is triggered when needed, and most of the time, this is just fine. But it could be a problem when your app is running in a resource limited environment.

If Node thinks it has more memory than it does, GC wont be triggered until it's too late and the process is terminated before it has a chance to clean itself up. In my case, I suspect what Node "thinks" is tied to how Google manages memory allocation with soft and hard limits.

Regardless, the "fix" is to tell Node to start thinking about GC when you still have head room. You do this with --max-old-space-size.

{
    "start:prod": "node dist/main --max-old-space-size=256"
}

The caveat being that the lower you set it, the more often GC will be triggered, which will have an impact on performance. So, buyer beware. If your app needs the memory to run, this wont work for you. If it doesn't, and Node is just holding on to it, then this might do the trick.

The nest command

The default configuration of NestJS is not ideal for some production systems. GAE will invoke the start command when defined in package.json. Which means:

{
    "start": "nest start"
}

This is great, except for this:

nest start simply ensures the project has been built (same as nest build), then invokes the node command in a portable, easy way to execute the compiled application.

So, every time an instance of my API would be started, the project would be compiled and then run. Compiling is obviously a memory intensive process (in addition to being totally unnecessary). NestJs already provides the correct command start:prod, but I (and Google) assumed the naked start was the call to invoke. Probably others make the same assumption. So do this instead:

{
    "start": "npm run start:prod",
    "start:prod": "node dist/main",
}

This isn't scientific, but by doing these two things, my idle memory usage went from nearly 300mb to 113mb. And more importantly, I was able to drop from an F2 to F1 instance as a result.

1
  • This is awesome - I went from ~260MB to ~30MB - thank you!
    – arsis-dev
    Nov 3, 2022 at 10:46
3

Are you assessing memory after running "nest start --watch" (aka npm run start:dev in the Nest projects), or are you assessing memory after build and running the server with "node dist/main"? Because I was experiencing the same issue you describe - 1.2 GB memory consumption right after running nest start --watch. However, node dist/main resulted in memory usage of 150 MB.

1
  • I wish I would have seen this answer before I wrote mine! This is the correct answer. See stackoverflow.com/a/72124484/4151208 for more context about why this is the right answer (and another option to reduce the memory footprint too) Aug 9, 2022 at 17:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.