Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accumulating files in memory #365

Open
Pooyahmti opened this issue Aug 28, 2024 · 2 comments
Open

Accumulating files in memory #365

Pooyahmti opened this issue Aug 28, 2024 · 2 comments
Labels

Comments

@Pooyahmti
Copy link

Pooyahmti commented Aug 28, 2024

Running below code, with evey request, the app memory usage ( RSS ) increases until server runs out of memory.
Why the buffers are not garbage collected after response is sent ?

import http from 'http'
import busboy from 'busboy'

http.createServer((req, res) => {

  if (req.method === 'POST') {
    const bb = busboy({ headers: req.headers });
    bb.on('file', (name, file, info) => {
      const chunks = [];
      file.on('data', (data) => {
        chunks.push(data);
      })
      file.on('end', () => {
        console.log(info, Buffer.concat(chunks));
      })
    })
    bb.on('close', () => {
      res.writeHead(200, { Connection: 'close' });
      res.end("Data Received")
    });
    req.pipe(bb);
  }
}).listen(3000, () => {
  console.log("Listening for requests")
})
@mscdex
Copy link
Owner

mscdex commented Aug 28, 2024

Because V8 has a lazy garbage collector.

If you set the --expose-gc CLI flag you can explicitly trigger the V8 GC with global.gc(), but that should only be for troubleshooting and not for production use. For controlling memory you're better off using the other V8 flags that dictate various maximum memory usages.

@Pooyahmti
Copy link
Author

Manually calling the garbage collector had no effect. I suspect that something holds a strong reference to buffers, preventing them from being garbage collected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants