Go 是否会遭受与 Java 相同的微妙内存泄漏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4400311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:12:27  来源:igfitidea点击:

Is Go subject to the same subtle memory-leaks that Java is?

javamemory-leaksgo

提问by SyntaxT3rr0r

Here are the facts:

以下是事实:

  • the language Go has a garbage collector.

  • Java has a garbage collection

  • a lot of Java programs have (subtle or not) memory leaks

  • Go 语言有一个垃圾收集器。

  • Java 有垃圾回收机制

  • 许多 Java 程序都有(微妙或不明显)内存泄漏

As an example of a Java program that has memory leaks (not for the faint of heart, the question may shake your beliefs), see here about a little Java program called Tomcat that even has a "find leaks" button: Is there a way to avoid undeployment memory leaks in Tomcat?

作为一个有内存泄漏的 Java 程序的例子(不适合胆小的人,这个问题可能会动摇你的信念),请看这里关于一个名为 Tomcat 的小 Java 程序,它甚至有一个“查找泄漏”按钮:有没有办法避免 Tomcat 中的未部署内存泄漏?

So I am wondering: will programs written in Go exhibit the same kind of (subtle or not) memory leaks that some programs written in Java exhibit?

所以我想知道:用 Go 编写的程序是否会表现出与一些用 Java 编写的程序所表现出的相同类型(微妙或不明显)的内存泄漏?

回答by james

You are confusing different types of memory leaks here.

您在这里混淆了不同类型的内存泄漏。

The heinous, explicit-memory-management based memory leaks are gone in Java (or any other GC based language). These leaks are caused by completely losing access to chunks of memory without marking them as unused.

令人发指的、基于显式内存管理的内存泄漏在 Java(或任何其他基于 GC 的语言)中消失了。这些泄漏是由于完全失去对内存块的访问权限而没有将它们标记为未使用而引起的。

The "memory leaks" still present in Java and every other language on the face of the planet until the computer can read our minds are still with us, and will be for the foreseeable future. These leaks are caused by the code/programmer keeping references to objects which are technically no longer needed. These are fundamentally logic bugs, and cannot be prevented in any language using current technologies.

在计算机可以读取我们的思想之前,Java 和地球上所有其他语言中仍然存在“内存泄漏”,并且在可预见的未来仍然存在。这些泄漏是由代码/程序员保留对技术上不再需要的对象的引用造成的。这些基本上是逻辑错误,使用当前技术无法通过任何语言来防止。

回答by Poindexter

It's very possible that Go programs will exhibit memory leaks. The current implementation of Go has a simple mark-and-sweep garbage collector. This is only intended as a temporary solution and is not intended as the long term garbage collector. See this pagefor more info. Look under the header Go Garbage Collector. That page even has a link to code for the current version if you are so inclined.

Go 程序很可能会出现内存泄漏。Go 的当前实现有一个简单的标记和清除垃圾收集器。这仅用作临时解决方案,而不用作长期垃圾收集器。请参阅此页面了解更多信息。查看标题下Go Garbage Collector。如果您愿意,该页面甚至还有指向当前版本代码的链接。

回答by DJClayworth

A 'memory leak' is when a piece of memory that the programmer thought would be freed isn't freed. This can happen in any language, garbage collected or not. The usual cause in GC languages is retaining an additional reference to the memory.

“内存泄漏”是指程序员认为会被释放的一段内存没有被释放。这可以在任何语言中发生,垃圾收集与否。GC 语言中的常见原因是保留对内存的额外引用。

"Languages don't cause memory leaks, programmers cause memory leaks".

“语言不会导致内存泄漏,程序员会导致内存泄漏”。

回答by jzd

Garbage collection or not, you can write a program that has memory-leaks in Java, Go, or any other language for the most part.

不管垃圾收集与否,您都可以编写一个在 Java、Go 或任何其他语言中大部分情况下存在内存泄漏的程序。

Garbage Collection does take some of the burden off the programmer but it does not prevent leaks entirely.

垃圾收集确实减轻了程序员的一些负担,但它并不能完全防止泄漏。

回答by florin

You are mixing abstraction levels here: the memory leaks are due to bugs in the library (where objects reference each other though chains of 'a holds reference to b' as well as a trade-off in the implementation of the garbage collector between efficiency and accuracy. How much time do you want to spend on finding out such loops? If you spend twice as much, you'll be able to detect loops twice as long.

您在这里混合了抽象级别:内存泄漏是由于库中的错误(其中对象通过“a 链持有对 b”的引用以及垃圾收集器实现中的效率和准确度。你想花多少时间来找出这样的循环?如果你花两倍的时间,你将能够检测两倍的循环时间。

So the memory leak issue is not programming language specific, there is no reason that by itself GO should be better or worse than Java.

所以内存泄漏问题不是特定于编程语言的,没有理由认为 GO 本身应该比 Java 更好或更差。