节点JS createWriteStream内存泄漏

时间:2015-09-17 22:57:41

标签: javascript node.js csv memory-leaks

我有一个节点js过程,在读取数据库表中的10K行后,将行写入CSV文件。这些行已成功写入CSV文件,但之后节点js的内存使用量大幅增加,并且永远不会再次下降。我怀疑在以下代码中有(大)内存泄漏但无法找到它。

要编写此代码,我接受了How to use drain event of stream.Writable in Node.js的指导。代码执行“drain”子句,&没有错误消息,但仍然存在内存泄漏问题。

关于内存泄漏原因的任何想法?

exports.csv_export_ar = function(req, res)
{
  global.mysqlPool.getConnection(function(err,connection) {
  if(err)
      console.log( "Erge:Export:AR:Error:getConnection failed:%s", err );
  else {
      var call_proc = "call export_recent_core_events_ar(" + connection.escape(req.params.coreguid) + ");";

      connection.query( call_proc, function(err,the_data) {
          if(err) {
                console.error("Erge:CsvExport:AR:Error:Calling export_recent_core_events_ar:%s", err ); 
          }
          else {                  
                  var filename = '/tmp/export_ar_' + the_coreguid + '.csv';
                  var ws = fs.createWriteStream(filename);

                  ws.on('finish', function () {
                      console.log('Erge:Export:File has been written');
                    });

                  // Add this to ensure that the out.txt's file descriptor is closed in case of error.
                  ws.on('error', function(err) {
                    console.log('Erge:Export:Error:%s', err);
                    ws.end();
                  });

                  ws.write( 
                          'datetime,a0,a1,a2,a3,a4,a5,a6,a7,' +
                          'pin_state,mode_state,prov_battery,prov solar,prov grid,' + 
                          'use battery,use hwc,use other,' +
                          'battery charge,prov solar model,prov grid model,use grid model \r\n' );

                  var i = 0;
                  exportLines();

                  function exportLines() {
                    var ok = true;
                    do {
                      var the_row = the_data[0][i];
                      var the_line =
                          the_row.created_on  + ',' + 
                          the_row.a0 + ',' + the_row.a1 + ',' + the_row.a2 + ',' + the_row.a3 + ',' + 
                          the_row.a4 + ',' + the_row.a5 + ',' + the_row.a6 + ',' + the_row.a7 + ',' + 
                          the_row.pin_state + ',' + the_row.mode_state + ',' +  
                          the_row.wProvBatteryRaw + ',' + the_row.wProvSolarRaw + ',' + the_row.wProvGridRaw + ',' + 
                          the_row.wUseBatteryRaw + ',' + the_row.wUseHwcRaw + ',' + the_row.wUseOtherRaw + ',' +
                          the_row.battery_charge + ',' +
                          the_row.wProvSolarModel + ',' + the_row.wProvGridModel + ',' + the_row.wUseGridModel + 
                          ' \r\n';
                      ok = ws.write(the_line);
                      i++;
                    } while (i < the_data[0].length && ok);

                    if (i < the_data[0].length) {
                        // had to stop early!
                        // write some more once it drains. 
                        // If we don't wait for the drain event then the memory usage was increased
                        ws.once('drain', exportLines);
                    }
                    else
                    {
                        ws.end();           
                        ws = null;
                    }
                  }
              }
          }

          connection.release( );
      });
     }
   });
 };

0 个答案:

没有答案