资源泄漏与内存泄漏和性能之间的关系(the relation between resource leaks and Memory leaks and performance)

编程入门 行业动态 更新时间:2024-10-24 17:22:05
资源泄漏与内存泄漏和性能之间的关系(the relation between resource leaks and Memory leaks and performance)

资源泄漏我指的是Streams,StreamWriter(我认为他们正在使用文件描述符),Handles(GDI或用户也是Graphics字体)。 很快所有Closable对象都可以算作资源! 如果应用程序中存在一些资源泄漏。 假设一些InputStreams没有关闭,它们是否也可能因为垃圾收集器从内存中删除它们而导致内存泄漏?

另一个问题:资源泄漏会影响性能吗?

With resource leaks I mean Streams, StreamWriter (I suppose they are consuming File descriptors), Handles (GDI or user also Graphics fonts). Shortly all Closable objects can count as a resource! If there are some resource leaks in the application. Say some InputStreams are not getting closed, are they potential memory leaks too because Garbage Collector is not going to remove them from memory?

Another question: Are resource leaks affecting the performance?

最满意答案

这取决于你所谓的表现。 我假设你说的是整体性能,这意味着内存消耗,速度等都很重要。

它也取决于所使用的资源。 进程退出时会恢复某些资源(例如文件句柄),因此泄漏只会在执行时出现问题。 其他(如服务器或数据库连接)即使在您的应用程序执行后仍可能保持泄漏。 其他人(如互斥体等)应该尽快释放。

现在,后果取决于资源。 如果资源是同一进程中的本机对象,那么泄漏它可能会泄漏相关的内存。 如果资源是您锁定但未能解锁的互斥锁,那么您可能即将使应用程序死锁。 如果资源是连接,则即使您停止使用该服务器,服务器也会保持该连接处于打开状态。 如果资源是文件,它可能会阻止其他应用程序(甚至您自己的应用程序)再次访问它。

最后,虽然有些资源可能泄露,但其他资源不应该泄露。 就我而言,任何资源都不应该被泄露,但YMMV。

因此,您应该养成始终正确释放所获资源(内存,文件,连接,互斥体等)的习惯,无论该资源的重要性如何。 这样做将训练您正确的编码模式(和思维模式)。

如果您想探索这些概念, RAII和Exception Safety是您要搜索的关键字。

对于C#,将需要Dispose模式 (IDisposable接口和终结器)和using关键字。 另一种解决方案是使用try / finally的finally类来释放资源,但这很难正确维护。

在Java中,您将需要Java 7(IIRC),并使用AutoCloseable接口和“ try-with-resources ”语句。 与在C#中一样,您可以使用try / finally的finally类来释放资源,但问题相同。

It depends on what you call performance. I'll assume you're speaking of overall performance, meaning that memory consumption, speed, and the like are all important.

It depends, too, on the resource used. Some resources (e.g. file handles) are recovered when the process exits, so the leak will only be a problem when executing. Others (like server or database connections) could remain leaking even after your application execution. Others (like mutexes, etc.) should be released as soon as possible.

Now, the consequences depend on the resource. If the resource is a native object in the same process, then leaking it will probably leak the associated memory. If the resource is a mutex you locked but failed to unlock, then you are probably about to deadlock your application. If the resource is a connection, the server will keep that connection open even after you stopped using it. If the resource is a file, it could stop other applications (or even your own application) to access it again.

In the end, while some resources could be leaked, other shouldn't. As far as I am concerned, no resource should be leaked, ever, but YMMV.

So you should make an habit of always correctly releasing the resources you acquired (memory, files, connections, mutexes, etc.), no matter the perceived importance of that resource. Doing so will train you in the right coding patterns (and mindset).

RAII and Exception Safety are the keyword you're searching for if you want to explore the notions.

For C#, the Dispose pattern (IDisposable interface, and the finalizer) and the using keyword will be needed. Another solution is to use the finally class of a try/finally to free your resource, but this is difficult to maintain correctly.

In Java, you'll need Java 7 (IIRC), and use the AutoCloseable interface and the "try-with-resources" statement. As in C#, you can use the finally class of a try/finally to free your resource, with the same problems.

更多推荐

本文发布于:2023-08-04 16:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   性能   关系   资源   relation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!