为什么 Node.js 有增量内存使用? [英] Why does Node.js have incremental memory usage?

查看:52
本文介绍了为什么 Node.js 有增量内存使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超过 100 KB 的 gameserver.js 文件.每次刷新浏览器后,我都会检查我的任务管理器,并一直看到我的 node.exe 内存使用量在每次刷新时都在增加.我在这里使用 ws 模块:.

您应该设置一个时间间隔,将内存使用情况转储到一个文件中,即每秒一次,然后在几秒钟内对您的应用程序施加一些压力"(即对于 Web 服务器,发出数千个请求).然后看看结果,看看内存是一直在增加还是在遵循稳定的增加/减少模式.

检测内存泄漏的来源

最好的工具可能是 node-heapdump.您可以将它与 Chrome 调试器结合使用.

  1. 启动您的应用程序并施加初始压力(这是为了生成优化的代码并预热"您的应用程序)
  2. 当应用空闲时,生成一个堆转储
  3. 执行您怀疑可能会导致内存泄漏的单个附加操作(即,另一个请求) - 这可能是最棘手的部分,尤其是对于大型应用程序
  4. 生成另一个堆转储
  5. 将两个堆转储加载到 Chrome 调试器中并进行比较 - 如果存在内存泄漏,您将看到在该单个请求期间分配了一些对象,但之后没有释放
  6. 检查对象以确定发生泄漏的位置

我有机会调查了 Sails.js 框架中报告的内存泄漏 - 您可以在 这个问题.

还有一篇关于 StrongLoop 使用堆转储的详细文章 -建议去看看.

I have a gameserver.js file that is well over 100 KB in size. And I kept checking my task manager after each refresh on my browser and kept seeing my node.exe memory usage keep rising for every refresh. I'm using the ws module here: https://github.com/websockets/ws and figured, you know what, there is most likely some memory leak in my code somewhere...

So to double check and isolate the issue I created a test.js file and put in the default ws code block:

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({ port: 9300 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });
});

And started it up:

Now, I check node.exe's memory usage:

The incremental part that makes me confused is:

If I refresh my browser that makes the connection to this port 9300 websocket server and then look back at my task manager.. it shows:

Which is now at: 14,500 K.

And it keeps on rising upon each refresh, so theoretically if I keep just refreshing it will go through the roof. Is this intended? Is there a memory leak in the ws module somewhere maybe? The whole reason I ask is because I thought maybe in a few minutes or when the user closes the browser it will go back down, but it doesn't.

And the core reason why I wanted to do this test because I figured I had a memory leak issue in my personal code somewhere and just wanted to check if it wasn't me, or vice versa. Now I'm stumped.

解决方案

Seeing an increased memory footprint by a Node.js application is completely normal behaviour. Node.js constantly analyses your running code, generates optimised code, reverts to unoptimised code (if needed), etc. All this requires quite a lot of memory even for the most simple of applications (Node.js itself is from a large part written in JavaScript that follows the same optimisations/deoptimisations as your own code).

Additionally, a process may be granted more memory when it needs it, but many operating systems remove that allocated memory from the process only when they decide it is needed elsewhere (i.e. by another process). So an application can, in peaks, consume 1 GB of RAM, then garbage collection kicks in, usage drops to 500 MB, but the process may still keep the 1 GB.

Detecting presence of memory leaks

To properly analyse memory usage and memory leaks, you must use Node.js's process.memoryUsage().

You should set up an interval that dumps this memory usage into a file i.e. every second, then apply some "stress" on your application over several seconds (i.e. for web servers, issue several thousand requests). Then take a look at the results and see if the memory just keeps increasing or if it follows a steady pattern of increasing/decreasing.

Detecting source of memory leaks

The best tool for this is likely node-heapdump. You use it with the Chrome debugger.

  1. Start your application and apply initial stress (this is to generate optimised code and "warm-up" your application)
  2. While the app is idle, generate a heapdump
  3. Perform a single, additional operation (i.e. one more request) that you suspect will likely cause a memory leak - this is probably the trickiest part especially for large apps
  4. Generate another heapdump
  5. Load both heapdumps into Chrome debugger and compare them - if there is a memory leak, you will see that there are some objects that were allocated during that single request but were not released afterwards
  6. Inspect the object to determine where the leak occurs

I had the opportunity to investigate a reported memory leak in the Sails.js framework - you can see detailed description of the analysis (including pretty graphs, etc.) on this issue.

There is also a detailed article about working with heapdumps by StrongLoop - I suggest to have a look at it.

这篇关于为什么 Node.js 有增量内存使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆