Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: How memlab supports NodeJS memory leak detection #30

Open
foreseaz opened this issue Sep 20, 2022 · 5 comments
Open

Discussion: How memlab supports NodeJS memory leak detection #30

foreseaz opened this issue Sep 20, 2022 · 5 comments

Comments

@foreseaz
Copy link

Hi memlab devs, thanks for the great tool. I'm excited to try to integrate it into my current NodeJS app tests. I'm wondering what's current status or future road map is on supporting NodeJS memory leak detection.

Currently, most test cases and demos are running in browsers detecting front-end side memory leaks. Would you plan to give more support on NodeJS?

@JacksonGL
Copy link
Member

JacksonGL commented Sep 20, 2022

@foreseaz For node.js leak detection, you can take heap snapshots via takeNodeFullHeap, then use the following command to find memory leaks:

memlab find-leaks --baseline <FILE> --target <FILE> --final <FILE> --leak-filter <filter definition>

The built-in leak detector in MemLab is mainly for front-end memory leaks, so you need to defined a leak filter (API and example).

The most naive leak filter would be returning true for every objects allocated by target by never gets released, you can start from there and refine your self-defined leak detector for your node.js application.

@agaripian
Copy link

agaripian commented Oct 11, 2022

@JacksonGL thanks for documenting this. Few questions, can you share an example of a filter definition syntax I can try? Do I pass it the filter definition file? Can you share the syntax. Also when taking takeNodeFullHeap do I write that to a file and then use the cli to compare? Also what is baseline, target, and final? Is that just 3 separate heapdumps?

@JacksonGL
Copy link
Member

JacksonGL commented Oct 11, 2022

@agaripian

can you share an example of a filter definition syntax I can try?

Here are a few leak filter examples.

Do I pass it the filter definition file?

Yes

Leak filter documentation: https://facebook.github.io/memlab/docs/api/interfaces/core_src.ILeakFilter/

Also when taking takeNodeFullHeap do I write that to a file and then use the cli to compare?

takeNodeFullHeap will return an IHeapSnapshot object. You can use dumpNodeHeapSnapshot to write the heap snapshot to disk. The native API from node.js will also do the job.

Also what is baseline, target, and final? Is that just 3 separate heapdumps?

Yes, they are 3 separate heap snapshots, for more information please check out:
https://facebook.github.io/memlab/docs/how-memlab-works

baseline, target, and final corresponds to snapshot of A, B, and A' in the following animation:

@dennyak47
Copy link

@JacksonGL Thanks for the detailed reply.
I have a question about how to debug memory leak problem in nodejs.

When should I take the three snapshots of my leaked nodejs application?

In my case, the memory occupation keeps rising in my application.
It is hard to choose the 'baseline' and the 'final' status to take snapshots.
Unlike in the browser, there is no go-to-url and I can't reuse the thread and the heap.
Should I somehow stop my application without killing my nodejs main thread and take a final snapshot?
Would you kindly write a example?

@JacksonGL
Copy link
Member

JacksonGL commented Jun 20, 2023

@dennyak47 You can import and call this API to take heap snapshot from node.js:
https://facebook.github.io/memlab/docs/api/modules/heap_analysis_src/#takenodefullheap

In your case, just take three heap snapshots when the memory footprint is rising. For example, one heap snapshot when heap usage is 50MB (as baseline), another at 80MB (as target), and third heap snapshot at 100MB (as final).

If you are using the memlab find-leaks, something worth mentioning is that memlab's default leak detector is mainly written for detecting front-end JS leaks, so you may want to use memlab find-leaks --trace-object-size-above 1000000 to identify retained object with size over 1MB or large size.

Alternatively you can write your own leak detector for node.js:
https://facebook.github.io/memlab/docs/api/interfaces/core_src.IScenario/#-optional-leakfilter-leakfiltercallback

Here is an example on how to save the heap snapshot to disk, or to load the heap snapshot from disk:
https://facebook.github.io/memlab/docs/api/modules/core_src/#dumpnodeheapsnapshot

There are a few other heap anslysis commands that may be useful (check out by running memlab analyze -h)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants