Konubinix' opinionated web of thoughts

How to Find Idle Memory Leaks in Nodejs

Fleeting

I have read a lot about taking head dumps at separate interval and comparing them1. I tried this but it was difficult to actually find out what was wrong.

At some point, I discovered that the chrome inspector could produce much more meaningful reports when used with --inspect2

Once used correctly, it was very easy to pinpoint my memory leak.

Start the program with --inspect, then open chrome at chrome://inspect/. You should see the program in that interface. Then go to the memory tab, then “Allocation instrumentation on timeline” with the option “Record stack traces of allocations (extra performance overhead)”.

You will see memory getting allocated (blue bars) and freed (blue becoming gray). When you have enough, click on stop and take a look at the snapshot. The constructors will tell you what objects got allocated, the retainer panel will tell you why (the order is counter intuitive. I had to write them in a piece of paper to make sense of it).

And above all, click on “Allocation stack”. It will provide some very useful insight about where the allocation took place in your code.

In my case, it pinpointed exactly the culprit and fixing it was almost obvious from that point.

By the way, it seems that it is almost a matter of keeping listener open. In my case, a bullmq queue was opened and never closed.

So this:

const queue = new Queue()
dosomething

Simply became:

const queue = new Queue()
dosomething
await queue.close()