Node Multer memory storage: how to release memory

user3552178 picture user3552178 · Aug 21, 2017 · Viewed 8.4k times · Source

Ok multer supports memory storage, this is great.

  1. 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.

  1. Also what happens if 2 user uploading the files with same file name, what's going to happen to memory storage ?

Thanks !

Answer

EMX picture EMX · Aug 22, 2017
  1. 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)

  1. 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)