问题描述
以下是事实: 语言Go有一个垃圾收集器。
作为一个有内存泄漏的Java程序的例子这个问题可能会动摇你的信念),在这里看到一个叫Tomcat的小程序,它甚至有一个find leaks按钮:
所以我想知道:用Go编写的程序是否会展现出与内存泄漏相同的内容用Java编写的程序展现出来?
你在这里混淆了不同类型的内存泄漏。
内存泄漏仍然存在于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: Is there a way to avoid undeployment memory leaks in 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?
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.
这篇关于Go是否受到与Java相同的微妙内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!