CodexBloom - Programming Q&A Platform

Memory Leak When Using Node.js Streams with Large File Uploads in Multer

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-12
node.js multer file-upload memory-leak JavaScript

I'm deploying to production and I'm testing a new approach and I'm sure I'm missing something obvious here, but I'm experiencing a memory leak when handling large file uploads in my Node.js application using Multer for file parsing..... My application allows users to upload files up to 1GB, but after several uploads, the memory usage continues to grow, eventually crashing the server. I'm using Node.js v14.17.0 and Multer v1.4.2. I set up my upload handler as follows: ```javascript const express = require('express'); const multer = require('multer'); const app = express(); const upload = multer({ dest: 'uploads/' }); app.post('/upload', upload.single('file'), (req, res) => { res.send('File uploaded successfully'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` While testing, I noticed that after uploading a few large files, the memory usage of the Node.js process climbs significantly. I've tried using the `--max-old-space-size` flag to increase the memory allocation, but it doesn't seem to help. I'm also aware that streams can handle large files, so I attempted to use streams directly, but that resulted in a more complicated implementation. I would appreciate any advice on how to handle large file uploads without causing memory leaks or if I'm missing something in my current setup. Is there a better way to manage file uploads to prevent this memory scenario? I'm working on a application that needs to handle this. Has anyone else encountered this? I'm open to any suggestions. I'm developing on Ubuntu 20.04 with Javascript. This is for a mobile app running on Ubuntu 22.04. Could someone point me to the right documentation?