为什么异常会导致Node.js中的资源泄漏? [英] Why would an exception cause resource leaks in Node.js?

查看:136
本文介绍了为什么异常会导致Node.js中的资源泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您查看Node.js 域名文档的开头,请注明: / p>

If you look at the beginning of the Node.js documentation for domains it states:


由于JavaScript在JavaScript中的工作性质,几乎从来没有任何方法安全地拿起你离开的地方,没有泄漏引用或创建其他一些未定义的脆弱状态。

By the very nature of how throw works in JavaScript, there is almost never any way to safely "pick up where you left off", without leaking references, or creating some other sort of undefined brittle state.

再次在代码示例中,它在第一部分中提到:

Again in the code example it gives in that first section it says:


尽管我们防止突然重新启动,我们正在泄漏资源,如疯狂

Though we've prevented abrupt process restarting, we are leaking resources like crazy

我想了解为什么会这样?有什么资源泄漏?他们建议您只使用域来捕获错误并安全地关闭进程。这是所有例外的问题,而不仅仅是在使用域时?在Javascript中扔和捕获异常是不好的做法吗?我知道这是Python的一个常见模式。

I would like to understand why this is the case? What resources are leaking? They recommend that you only use domains to catch errors and safely shutdown a process. Is this a problem with all exceptions, not just when working with domains? Is it a bad practice to throw and catch exceptions in Javascript? I know it's a common pattern in Python.

编辑

我可以理解为什么如果抛出异常,可能会出现非垃圾回收语言的资源泄漏,因为如果抛出异常,您可能运行的清除对象的任何代码都不会运行。

I can understand why there could be resource leaks in a non garbage collected language if you throw an exception because then any code you might run to clean up objects wouldn't run if an exception is thrown.

我可以用Javascript来想象的唯一原因是如果抛出异常存储引用异常的范围的引用(也可能是调用堆栈中的事情),从而保留引用,然后保留异常对象周围,​​从不被清理。除非引用的泄漏资源是引擎内部的资源。

The only reason I can imagine with Javascript is if throwing an exception stores references to variables in the scope where the exception was thrown (and maybe things in the call stack), thus keeping references around, and then the exception object is kept around and never gets cleaned up. Unless the leaking resources referred to are resources internal to the engine.

更新

我已经写了一个博客,解释一下这个答案好一点。 查看

I've Written a blog explaining the answer to this a bit better now. Check it out

推荐答案

意外的例外是你需要担心的。如果您不了解应用程序的状态以添加特定异常的处理并管理任何必要的状态清理,那么根据定义,您的应用程序的状态是未定义的,不可知的,很可能有事情挂在周围不应该是。不仅仅是内存泄漏你不得不担心。未知的应用程序状态可能导致不可预测和不需要的应用程序行为(例如传递输出错误 - 部分渲染的模板或不完整的计算结果,或更糟的是,每个后续输出错误的情况)。这就是为什么在发生未处理的异常时退出进程很重要。它给你的应用程序修复自己的机会。

Unexpected exceptions are the ones you need to worry about. If you don't know enough about the state of the app to add handling for a particular exception and manage any necessary state cleanup, then by definition, the state of your app is undefined, and unknowable, and it's quite possible that there are things hanging around that shouldn't be. It's not just memory leaks you have to worry about. Unknown application state can cause unpredictable and unwanted application behavior (like delivering output that's just wrong -- a partially rendered template, or an incomplete calculation result, or worse, a condition where every subsequent output is wrong). That's why it's important to exit the process when an unhandled exception occurs. It gives your app the chance to repair itself.

异常发生,没关系。拥抱它。关闭该过程,并使用 Forever 这样的一些内容来检测并将事情重新设置。集群和域也是伟大的。您正在阅读的文本不是要谨慎,不要抛出异常,或者在处理您所期望的异常时继续此过程 - 在发生意外异常时,请务必保持该过程的运行。

Exceptions happen, and that's fine. Embrace it. Shut down the process and use something like Forever to detect it and set things back on track. Clusters and domains are great, too. The text you were reading is not a caution against throwing exceptions, or continuing the process when you've handled an exception that you were expecting -- it's a caution against keeping the process running when unexpected exceptions occur.

这篇关于为什么异常会导致Node.js中的资源泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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