Skip to content

[3.2.0] Potential memory leak #2631

@Cinderella-Man

Description

@Cinderella-Man

Recently I was stress testing my API and discovered that even very simple net server has a memory leaks. Is it possible that there's something wrong with it?

Server code:

"use strict"

var net = require('net');

// Keep track of the chat clients
var clients = [];

function clean() {
  global.gc();
  console.log('Current memory usage: ', process.memoryUsage());
  setTimeout(clean, 30000);
}

setTimeout(clean, 30000);

var proxy = net.createServer(function (socket) {

  // Identify this client
  socket.name = socket.remoteAddress + ":" + socket.remotePort

  // Put this new client in the list
  clients.push(socket);

  socket.on('close', function () {
    clients.splice(clients.indexOf(socket), 1);
    socket = null;
  });

  socket.on('error', function (err) {
    clients.splice(clients.indexOf(socket), 1);
    socket = null;
  });
});

console.log('Current memory usage: ', process.memoryUsage());

proxy.listen(2919);

Testing client bash script:

#!/bin/bash

for i in $(seq 1 1 10000)
do
  curl -m0.001 http://127.0.0.1:2919/;
done

Starting iojs server(3.2.0):

$ node --expose-gc test.js
Current memory usage:  { rss: 21942272, heapTotal: 9275392, heapUsed: 3732936 }
Current memory usage:  { rss: 36179968, heapTotal: 26806272, heapUsed: 4428112 }
Current memory usage:  { rss: 47865856, heapTotal: 44349184, heapUsed: 4496016 }
Current memory usage:  { rss: 47325184, heapTotal: 40221440, heapUsed: 4472480 }

After 10k calls server memory consumption increased from 21 to 45.5MB. Is that scripts' fault?

Metadata

Metadata

Assignees

No one assigned

    Labels

    memoryIssues and PRs related to the memory management or memory footprint.netIssues and PRs related to the net subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions