Node.js的流API泄漏 [英] Node.js Stream API leak

查看:153
本文介绍了Node.js的流API泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与节点流打我注意到,pretty每很多教程教是这样的:

while playing with node streams I noticed that pretty much every tutorial teaches something like:

// Get Google's home page.
require('http').get("http://www.google.com/", function(response) {
  // The callback provides the response readable stream.
  // Then, we open our output text stream.
  var outStream = require('fs').createWriteStream("out.txt");

  // Pipe the input to the output, which writes the file.
  response.pipe(outStream);
});

但是,这是一个pretty危险一块code,在我看来。如果文件流抛出异常的某个时候会发生什么?我想是因为根据文档文件流显然不是关闭文件流可以导致内存泄漏。

But this is a pretty dangerous piece code, in my opinion. What happens if the file stream throws an exception an some point? I think the file stream could leak memory because according to the docs the file stream is obviously not close.

我要在乎?在我的选择的node.js流应该处理的情况下...

Do I should care ? In my option node.js streams should handle situations...

推荐答案

霸菱在节点的VM任何错误,如果该流已被打开后,中断运行异常,我期望最终在垃圾回收虚拟机将检测,没有什么是指流和将收集它,从而与它相关联的资源的处理。

Baring any bugs in Node's VM, if there is an exception that interrupts the operation after the stream has been opened, I'd expect that eventually during garbage collection the VM would detect that nothing is referring to the stream and would collect it, thereby disposing of the resources associated with it.

所以我不会把它称为泄漏。

So I would not call it a "leak".

有仍然可以使用的的处理异常,或不关闭流相关的问题。例如,在当一个流被创建对应于磁盘上的文件的Unix类型的系统中,使用一个文件描述符。有多少文件描述符可以在同一时间通过的处理被打开的限制。因此,如果这一过程没有明确关闭其流的管理留下这么多,这些未闭合的,它的下一个垃圾回收之前打文件描述符限制,它会崩溃。

There can still be problems associated with not handling exceptions, or not closing streams. For instance, on Unix-type systems when a stream is created that corresponds to a file on disk, a file descriptor is used. There's a limit to how many file descriptors can be opened at one time by a process. Consequently, if a process which does not explicitly close its streams manages to leave so many of these unclosed that it hits the file descriptor limit before the next garbage collection, it will crash.

这篇关于Node.js的流API泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆