mafintosh / torrent-stream

The low level streaming torrent engine that peerflix uses
MIT License
1.94k stars 227 forks source link

Memory leak when downloading big files #165

Open mortie opened 8 years ago

mortie commented 8 years ago

Hi. I'm working on a project to stream video in torrent files, and have encountered an issue: the more of a movie is downloaded, the more memory is used, sometimes resulting in a crash due to out of memory errors. I've taken a heap snapshot, and it points to the bencode library, which I know your library usues.

My project is here: https://github.com/mortie/mmpc-media-streamer/. The file which uses torrent-stream: https://github.com/mortie/mmpc-media-streamer/blob/master/js/kickass-proxy/streamer.js

The heap snapshot file is too big for github, so I put it on my web server: https://d.mort.coffee/file/heapsnapshot.gz

Gunzip that and load it in chrome's profiler, and you'll see system / JSArrayBufferData uses gigabytes of memory. I have very little experience interpreting heap snapshots, but the only file name I found was node_modules/bencode/lib/decode.js

sploadie commented 8 years ago

I may be wrong, but it seems you replace the functionality of file.destroy() here:

media.destroy = function() {
  engine.destroy();
  engine = null;
}

Which was originally:

FileStream.prototype.destroy = function () {
  if (this._destroyed) return
  this._destroyed = true
  this.emit('close')
}

As defined by torrent-stream/lib/file-stream.js. Assuming this prevents memory from being freed, this may be you issue. Hope this helps.