Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
IncomingMessage,ServerResponse not garbage collected in keep-alive connections

In the last few days, I've investigated a memory leak issue regarding keep-alive HTTP/1.1 connections in NodeJS 14.17.3.

I have simplified the implementation of the API as shown below.

async function index(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    Connection: 'keep-alive',
    'Access-Control-Allow-Credentials': 'true',
    'Access-Control-Expose-Headers':
      'Accept,Accept-Encoding,Accept-Language,Cache-Control,Connection,Host,Origin,Pragma,Referer,Sec-Fetch-Dest,Sec-Fetch-Mode,Sec-Fetch-Site,Sec-GPC,User-Agent',
  });

  res.write('test');

  res.on('close', () => {
    res.end();
  });
}

The issue appears when the client interrupts the connection from his side. Although the 'close' event is fired and the callback is executed successfully, the objects remain in memory. Using the inspect tool of chrome, I took a picture and attached it below.

Memory Snapshot

What I expect is the highlighted ServerResponse object to be garbage collected after the response ends. Also, the 'finish' event is not fired in this case. I do not know if it is relevant or if it is a standard behavior when the request is interrupted by the client.

I use expressjs as the web framework and the reason the connections are 1.1 is that the API is for server-sent events.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!