为什么Rust认为泄漏内存很安全? [英] Why does Rust consider it safe to leak memory?

查看:690
本文介绍了为什么Rust认为泄漏内存很安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Rust Book中的这一章,可以通过创建一个指针循环来泄漏内存:

According to this chapter in the Rust Book, it is possible to leak memory by creating a cycle of pointers:

锈迹斑斑的内存安全保证使意外创建从未清除过的内存变得困难,但并非不可能(称为内存泄漏).完全防止内存泄漏并不是Rust的保证之一,就像在编译时禁止数据竞争一样,这意味着内存泄漏在Rust中是内存安全的.我们可以看到,Rust通过使用Rc<T>RefCell<T>允许内存泄漏:可以创建引用,其中各个项目在一个周期中相互引用.这会造成内存泄漏,因为循环中每个项目的引用计数将永远不会达到0,并且这些值也永远不会被丢弃.

Rust’s memory safety guarantees make it difficult, but not impossible, to accidentally create memory that is never cleaned up (known as a memory leak). Preventing memory leaks entirely is not one of Rust’s guarantees in the same way that disallowing data races at compile time is, meaning memory leaks are memory safe in Rust. We can see that Rust allows memory leaks by using Rc<T> and RefCell<T>: it’s possible to create references where items refer to each other in a cycle. This creates memory leaks because the reference count of each item in the cycle will never reach 0, and the values will never be dropped.

存在诸如弱指针"之类的替代方法,这些替代方法使您可以创建自引用结构,这些结构在删除后仍可以清除.实际上,本章稍后会建议使用Weak<T>.

There exist alternatives like "weak pointers" that would allow you to create self-referential structures that could still be cleaned up when dropped. In fact, using Weak<T> is actually suggested later in that chapter.

为什么Rust认为这很安全?为什么在这种情况下,该语言没有采取任何措施来防止不良的程序员行为"?

Why does Rust consider this safe? Why is this an instance where the language does not do anything to prevent 'bad programmer behaviour'?

推荐答案

因为它很安全.

unsafe在Rust中具有非常特殊的含义,它专门针对触发 Undefined Behavior 的编程错误类别.这些是最令人讨厌的错误,因为它们完全破坏了您对程序的整体理解,从而使编译器或硬件都无法预测地运行.

unsafe has a very specific meaning in Rust, it specifically targets classes of programming mistakes which trigger Undefined Behavior. Those are the nastiest mistakes, as they completely subvert your whole understanding of a program, allowing either compiler or hardware to behave in unpredictable ways.

内存泄漏不会触发未定义行为,因此很安全.

Memory leaks do not trigger Undefined Behavior, and therefore are safe.

您可能对Nomicon(相当于Rust Book的不安全版本)对泄漏;关于ScopeGuard的示例通常被称为泄漏启示录".

You may be interested in what the Nomicon (the Unsafe equivalent of the Rust Book) has to say about Leaking; the example about ScopeGuard is often referred to as the Leakpocalypse.

值得注意的是,例如,垃圾收集的语言很容易泄漏内存.一个简单的Map,其中添加键-值对而没有删除它们将最终导致堆耗尽;并且GC将无法停止它.

It is notable that Garbage Collected languages can easily leak memory, for example. A simple Map in which key-value pairs are added without ever being removed will eventually lead to heap exhaustion; and the GC will not be able to stop it.

不断增长的Map就像反复忘记使用free指针一样不受欢迎,无论哪种情况,堆耗尽都将出现,但GC语言通常被认为是安全的.

An ever-growing Map is as undesirable as repeatedly forgetting to free a pointer, in either case heap exhaustion looms, yet GC'ed languages are considered safe in general.

这篇关于为什么Rust认为泄漏内存很安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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