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/
分享到:
相关推荐
- **MAT (Memory Analyzer Tool)**:Eclipse的一个插件,适用于Java应用,可以深入分析Hprof文件,找出可能的内存泄漏。 - **Android Dev Options**:在设备上启用“开发者选项”中的“严格模式”,可以直观地看到...
然后: export DNS_NODEJS_TEST_HOST {{some host}}export DNS_NODEJS_TEST_IP {{some ip}} 有一个npm run echo-server展示了如何使用 ncat 来设置一个回声服务器。 一旦你设置了一个 UDP 回显服务器,你就可以运行...
Memory Leak 是最难排查调试的 Bug 种类之一,因为内存泄漏是个 undecidable problem,只有开发者才能明确一块内存是不是需要被回收。再加上内存泄漏也没有特定的报错信息,只能通过一定时间段的日志来判断是否存在...
Additionally we've fixed a long standing memory leak which caused users to have to restart Komodo every few days, that should no longer be the case. - Other Mentionables - Improved source code ...
StackImpact Node.js探查器总览StackImpact是为生产和开发环境构建的生产级性能分析器。 它为开发人员提供了应用程序性能的连续和历史代码级视图,这对于查找CPU,内存分配和I / O热点以及延迟瓶颈至关重要。...
当在内存不足的环境中运行nodejs进程时,发生的每一次内存不足都是很有趣的。 为了弄清为什么进程内存不足,堆快照(例如heapdump)可以提供很多帮助。 该模块将在发生内存不足错误之前(通过利用V8引擎的'...
泄漏-节点的内存泄漏测试 使用Mocha或您选择的其他测试运行程序编写泄漏测试。 不仅支持发现和修复内存泄漏,而且编写测试还使您能够防止回归并显示代码不会泄漏。 目录 安装 npm install --save-dev leakage ...
简易监控3.0 ... ... 3.0简介 全新的企业级Node.js应用性能监控与在线故障定位解决方案。 推出Node.js的开源生态工具链上做一些微小的事情,希望能帮助到使用和正在使用Node.js的开发者更好地感知自己的Node.js应用状态,...