上传文件时内存泄漏

时间:2015-07-14 07:48:22

标签: node.js formidable

我正在尝试使用强大的内容上传文件(30GB)。每当上传开始时,ram会急剧增加,而且几乎使用99%的内存。 这是我的上传逻辑..

app.post('/upload', function (req, res) {
var path;
var randomFilename;
var UUID;
var form = new formidable.IncomingForm();
encodingModule.generateUUID(function(uuid){
    UUID = uuid;
});
form.on('fileBegin', function(name, file) {
  console.log(file.name);      
  file.path = null;
  randomFilename = UUID+'.'+file.name.split('.').pop();
  path = file.path = "./ContentCache/"+randomFilename;
});

form.on('end', function(){
    encodingModule.getMetadata(path,function(data){   
         parseApp.update('ContentFile', req.query.objectId, {'fileId':randomFilename, 'videoCodec':data.streams[0].codec_name, 'videoBitrate':data.format.bit_rate, 'audioCodec':data.streams[1].codec_name, 'audioBitrate':data.streams[1].bit_rate, 'resolutionX':data.streams[0].width, 'resolutionY':data.streams[0].height}, function(err,response){
            if(err){
                console.log("metadata:error updating object");
            }
            console.log(response);
         }); 
    });     
    console.log('Local: Upload Successfull..!');
    res.send({msg: "Local: Successfully Uploaded!", objectId: req.query.objectId}); 
    videoQueue.add({video: path, filename:randomFilename, objectId:req.query.objectId});  
}); 

//On upload error
form.on('error', function(err) {
  console.log('Local: Error occured while uploading: '+err);
});

//on abort
form.on('aborted', function(){
   console.log('Upload aborted'); 
});

//Azure upload
onFinished(req, function (err, req) {
         azureUpload(req.query.objectId, randomFilename, path, function(result){
            console.log(result);
    });       
});      

//Parse incoming nodejs request
form.parse(req);
});

在这里,我从我的PC中选择一个文件并将其上传到托管我的节点服务器的虚拟机。上传结束后进行编码(逻辑在另一个我们不想要的文件中)。所以每当上传开始ram使用量大幅增加15分钟内10%到99%。怎么解决这个问题?我的上传逻辑是否有任何问题。我使用express但我不使用body解析器

1 个答案:

答案 0 :(得分:0)

首先,您的机器有多少RAM?

我不熟悉formidable的内部结构,但也许你应该尝试打开文件StreamWriter并在文件进入时写入文件。

来自强大的文件:

form.onPart = function(part) {
  part.addListener('data', function() {
    // Here you should write 'part' to your StreamWriter
  });
}

这可能有助于从RAM中取出负载并将文件放在磁盘上

相关问题