1 of 11

NodeJS

Memory Management &

Memory Leak Detection

By Sina Sharafzadeh

2 of 11

Nodejs Memory stuff

  • NodeJS GC
  • Why my app use this much memory?
  • How much memory we can use?
  • How we can prevent reaching the limits?
  • Why we should use that much memory , a cpu intensive task🤔?
  • Let’s create some leakages
  • Chrome profiler detailed
  • Some suggestions

3 of 11

Nodejs Garbage Collection

Resident Set

Code Segment

Stack Segment

Old Pointer Space ...

Heap Size

Call Stack

4 of 11

Why my app use this much memory?

It’s really under the load…(cpu intensive call stack!)

Garbage collection not running yet

Memory leaks

Let’s allocate some memory

Examples ...

5 of 11

How much memory we can use?

Current limit is near 1.76GB

Let’s reach it and see examples …

6 of 11

How we can prevent reaching the limits?

Call the gc , when and how(pros and cons)?

Find the leakages

Scale your app

Optimize your code

7 of 11

Why we should use that much memory , a cpu intensive task🤔?

We can change the limits 😁😁

Take care of why your memory growth

8 of 11

Let’s create some leakages

9 of 11

Let’s create some leakages ...

Logical leakages

New instance leakages

...

10 of 11

Chrome profiler detailed

Attach to your node app

At start with arguments

At runtime (kill -SIGUSR1 + node-inspect)

To running instance on server

11 of 11

Some Suggestions

  • Don’t add large files to memory
  • Do not block main server thread
  • Do not create unnecessary data
  • Know your tools