8

I want to know how I can destroy a Buffer in order to free memory.
I have the code below, it creates a Buffer and send this one as response. This works fine but when I play with a big array like with 75 000 rows, I can see my memory takes over 1Go, it's ok but when the response is sent, this memory is kept and not free... I try to set var buffer to null at the end of the script but nothing append... Is there a solution to free this memory ?

var xlsxexport = require('node-xlsx');

module.exports = {
    exportExcel: function (req, res) {

        var excelData = []
        // ...
        // Construction of the array excelData
        // ...

        var buffer = xlsxexport.build([{name:'export', data:excelData}])
        res.set 'Content-Type', 'application/vnd.openxmlformats'
        res.set 'Content-Disposition', 'attachment; filename=' + filename
        res.send buffer
  }
}
Blo
  • 11,903
  • 5
  • 45
  • 99
Zagonine
  • 2,213
  • 3
  • 22
  • 29

2 Answers2

12

If you are really confident that your code has no leaks, and you already set every big variable to null, then you can try to start GC.

Add option --expose-gc when you are running you script:

node --expose-gc index.js

And whenever you want inside of you scripts you can call GC:

global.gc()

But I strongly recommend to you to find some ways to do it without forcing GC.

Good luck!

JerryCauser
  • 811
  • 5
  • 17
2

First, the module that you are using seems to cause a lot of overhead for 75k rows, wouldn't it be better to use CSV format instead?

If you do want to continue with your approach, there some V8 options that can restrict some memory limits. Restricting the memory limits could cause more time wasted on doing garbage collecting, so be careful with over optimising.

Here is good place to start with: Limit Node.js memory usage to less than 300MB per process

Community
  • 1
  • 1
Risto Novik
  • 8,199
  • 9
  • 50
  • 66