Memory Leak with socket.io + node.js

前端 未结 2 1665
野的像风
野的像风 2021-02-05 02:00

I appear to have a memory leak with my Node.js application. I built it quickly, and my JavaScript isn\'t too strong, so this might be easy.

I\'ve done some heap dumps on

相关标签:
2条回答
  • 2021-02-05 02:34

    Could this be related to the connected clients array not clearing properly when a client disconnects? The array value gets set to NULL rather than being dropped from the array.

    0 讨论(0)
  • 2021-02-05 02:36

    Do yourself a favour and use node-mysql, it's a pure javascript mysql client and it's fast. Other than that, you should be using asynchronous code to stop IO being blocked whilst you're working. Using the async library will help you here. It has code for waterfall callback passing among other things.

    As for your memory leaking, it probably isn't socket.io, although I haven't used it in a few months, I have had many thousands of concurrent connections and not leaked memory, and my code wasn't the best either.

    Two things, however. Firstly your code is faily unreadable. I suggest looking into properly formatting your code (I use two spaces for every indentation but some people use four). Secondly, printing the number of connections every half an hour seems a little silly, when you could do something like:

    setInterval(function() {
      process.stdout.write('Current connections: ' + connections + '     \r');
    }, 1000);
    

    The \r will cause the line to be read back to the start of the line and overwrite the characters there, which will replace the line and not create a huge amount of scrollback. This will help with debugging if you choose to put debugging details in your logging.

    You can also use process.memoryUsage() for quickly checking the memory usage (or how much node thinks you're using).

    0 讨论(0)
提交回复
热议问题