Ok multer supports memory storage, this is great.
but how to release the memory after uploading the file pls ?
storage = multer.memoryStorage();
upload = multer({ storage : storage }).single('image');
For disk storage, I can delete the file in the dest folder.
Thanks !
- but how to release the memory after uploading the file ?
As soon as you are done with the request, the memory will be freed.
(when the Garbage Collector (GC) cleans up, usually almost straight away)
- Also what happens if 2 user uploading the files with same file name, what's going to happen to memory storage ?
You don't have to worry since each file is being handled inside its own scope, this means, like I mentioned above, each request is handled and then, once its done the request is purged (along with the stored request.file from the memoryStorage)
I suggest you check how multer handles the memory storage multer/storage/memory.js (github)
MemoryStorage.prototype._handleFile = function _handleFile (req, file, cb) {
file.stream.pipe(concat(function (data) {
cb(null, {
buffer: data,
size: data.length
})
}))
}
as you can see the file is simply being streamed to the callback (cb), so there is no global references that would keep it from living in memory (no references to it = not needed anymore = garbage = GC)
The memoryStorage is mainly for handling the file buffer in a temporary way.
example : you want to receive small files and relay them to another server which stores the media. (multer receives stores in memory temp, relays and frees the memory by itself once theres no reference to it anymore)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With