Memory Leak in NodeJS
I have done a lot of memory leak investigation for JAVA, this is the first time for NodeJS.
Compare the version of TSC
> sudo npm install -g typescript@2.1.4
> tsc --version
Version 2.1.4
My current one is
> tsc --version
Version 3.2.2
> node --version
v10.14.2
In my package.json file, it start the nodeJS application like this
"start": "node build/src/index.js",
So I follow the docs and add - - inspect to that
"start": "node --inspect build/src/index.js",
Once it start, it will open a debug port
> npm run start
> sillycat-microservice@0.0.3 start /Users/hluo/company/code/sillycat.contactManager
> node --inspect build/src/index.js
Debugger listening on ws://127.0.0.1:9229/50c12fb6-488b-4456-b96a-a2f4e1f92582
For help, see: https://nodejs.org/en/docs/inspector
In the chrome browser, we can open a tab and put
chrome://inspect
We can open the monitor of that nodeJS process and we can take snapshot of the nodeJS memory to see what methods use most of the memory.
In my case, process.nextTick() happen a lot and use most of the memory
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
https://www.oschina.net/translate/understanding-process-next-tick
So after I read a lot of related documents. I guess maybe here is the reason.
I am using nodeJS to consume the messages from RabbitMQ, this fetch message operations are quick and powerful, but for each message, we need to do CRUD on DB and ElasticSearch. It seems heavier.
https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html
https://mariuszwojcik.wordpress.com/2014/05/19/how-to-choose-prefetch-count-value-for-rabbitmq/
So there is time window NodeJS app received a lot of messages, but the callback did not happen yet to ACK the message. We generate a lot of nextTick and host all these messages in our memory. These objects can not be GC.
Then I open the UI console in RabbitMQ, and I saw the messages on the fly are large. Every time the messages on the fly grows to a large number, I get OUT OF MEMORY in nodeJS console. And the last few GC logging showing nodeJS can not get any free memory after GC.
And sometimes the ‘deliver/get’ is reaching 2000 m/s but the ‘ack’ is still 15 m/s.
I think that means Consumer just try to fetch too many messages and can not handle them in time.
After I came to change my NodeJS codes
await channel.prefetch(10000); //limit 10000 message on the fly
Limit 10000 messages on the fly, it will not slow down the consuming of the rabbitMQ. But it will make the 'deliver/get' and ‘ack’ more stable and the same number
References:
https://sillycat.iteye.com/blog/772289
https://unix.stackexchange.com/questions/10106/orphaned-connections-in-close-wait-state
https://www.shellhacks.com/kill-tcp-connections-close-wait-state/
https://marmelab.com/blog/2018/04/03/how-to-track-and-fix-memory-leak-with-nodejs.html
https://mariuszwojcik.wordpress.com/2014/05/19/how-to-choose-prefetch-count-value-for-rabbitmq/
分享到:
相关推荐
memory leak 教程,一步一步解决内存泄漏。
This program is a tool that help you positioning memory leak locations of an QUALCOMM BREW application on simulator. It can give the call stack of memory leaks (including the locations that MALLOC/...
一个在模拟器上定位BREW应用程序内存泄露位置的工具,可以给出内存泄露位置的调用栈信息(包括MALLOC、REALLOC分配的内存,也能定位到调用ISHELL_CreateInstance的位置)
本文中描述了C++中常见的内存泄露,以及对应的解决方法,并且介绍了几种常见的内存泄露检查工具的使用(常见的工具只有vld和codesnitch)
memory leak tools for android memory leak tools for android
Memory leak detection for WinCE
NULL 博文链接:https://kaqi.iteye.com/blog/1391499
检测内存泄漏的工具源码 三个文件 使用时只要include一个头文件就可以 内含一个example程序
内存泄露(memory leak)资料,网上收集的,感谢原作者
C++ memory leak detection and exception
java
This program is a tool that help you positioning memory leak locations of an QUALCOMM BREW application on simulator. It can give the call stack of memory leaks (including the locations that MALLOC/...
有关flex 内存泄漏memory leak介绍的ppt. flash player的内存泄漏实在搞的我头大了
A memory leak detection library for Android and Java.
A Practical FlowSensitive and ContextSensitive C and C++ Memory Leak Detector
使用__wrap_malloc查找malloc导致内存泄漏问题 memory leak 内存泄漏
This document is about Memory leak.
技術分享_Glassfish 3.1.2.2 Web Service Memory Leak Workaround1
内存泄漏检测器 MemoryLeakDetector是由Xigua视频android团队开发的本地内存泄漏监视工具。 它具有访问简单,监视范围广,性能...leak-detector:0.0.7 '} 步骤3:添加代码以方便使用(使用广播控件不需要此步骤) //