英文:
Is Go subject to the same subtle memory-leaks that Java is?
问题
以下是翻译好的内容:
这里是事实:
-
Go语言有一个垃圾回收器。
-
Java有垃圾回收机制。
-
很多Java程序存在(微妙或不微妙的)内存泄漏问题。
作为一个Java程序的例子,它存在内存泄漏问题(对于胆小的人来说,这个问题可能会动摇你的信仰),可以在这里看到一个名为Tomcat的小Java程序,甚至还有一个“查找泄漏”按钮:https://stackoverflow.com/questions/4565105
所以我想知道:使用Go编写的程序是否会出现与一些使用Java编写的程序相同类型的(微妙或不微妙的)内存泄漏问题?
英文:
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
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: https://stackoverflow.com/questions/4565105
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?
答案1
得分: 46
你在这里混淆了不同类型的内存泄漏。
在Java(或任何其他基于GC的语言)中,那些可怕的、基于显式内存管理的内存泄漏已经消失了。这些泄漏是由于完全失去对内存块的访问权限,而没有将它们标记为未使用。
在Java和其他任何一种语言中,直到计算机能够读取我们的思维之前,仍然存在着“内存泄漏”,并且在可预见的未来仍将存在。这些泄漏是由代码/程序员保留对技术上不再需要的对象的引用所引起的。这些根本上是逻辑错误,无法通过当前技术在任何语言中防止。
英文:
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.
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.
答案2
得分: 19
很有可能Go程序会出现内存泄漏。Go的当前实现采用了简单的标记-清除垃圾收集器。这只是一个临时解决方案,并不是长期的垃圾收集器。更多信息请参考此页面。在Go Garbage Collector
标题下查看。该页面甚至提供了当前版本的代码链接,如果你有兴趣的话。
英文:
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 page for 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.
答案3
得分: 9
一个“内存泄漏”是指程序员认为会被释放的一块内存没有被释放。这可能发生在任何语言中,无论是否有垃圾回收机制。在有垃圾回收机制的语言中,通常的原因是保留了对内存的额外引用。
“语言不会导致内存泄漏,是程序员导致内存泄漏”。
英文:
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.
"Languages don't cause memory leaks, programmers cause memory leaks".
答案4
得分: 8
垃圾回收与否,你可以在Java、Go或其他大部分语言中编写一个存在内存泄漏的程序。
垃圾回收确实减轻了程序员的负担,但并不能完全防止内存泄漏。
英文:
Garbage collection or not, you can write a program that has memory-leaks in Java, Go, or any other language for the most part.
Garbage Collection does take some of the burden off the programmer but it does not prevent leaks entirely.
答案5
得分: 3
你在这里混淆了抽象级别:内存泄漏是由于库中的错误引起的(其中对象通过“a持有对b的引用”的链式引用以及垃圾回收器在效率和准确性之间的权衡实现)。你想花多少时间来查找这样的循环?如果你花费两倍的时间,你将能够检测到两倍长的循环。
因此,内存泄漏问题与编程语言无关,没有理由认为GO本身应该比Java更好或更差。
英文:
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论