90
votes

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?

5
"a lot of Java programs have (subtle or not) memory leaks" do you have any evidence of this "fact" or is it just a talking point. - Peter Lawrey
@Webinator: I think you need to give an example of one of these "subtle memory leaks" that "a lot of" Java programs have. Unless you are claiming a bug in the garbage collector, the only way to leak memory in pure Java is to hold on to references that you are no longer using e.g. by putting objects in a collection and never removing them from that collection. If this is the kind of leak you are referring to, then no language in the world will protect against them, including Go. - JeremyP
Here are the memory leaks I was talking about (+200 upvotes, answers with +150 upvotes, lots of actual Java memory leaks explained): stackoverflow.com/questions/6470651/… - SyntaxT3rr0r
Of course JAVA programs do have memory leaks, just forget to set some reference to null when you dot longer need them. Occurs frequently in large persistant collections (like caches), with observer design pattern or thread local variables. This is not theoretical, I corrected several memory leak myself in production. And this is not anti Java. I'am a full time JAVA dev for moren than 5 years, have been working on lot of different projects. Don't recognising you can have memory leak in JAVA (or all other existing language) is not fanboyism, is more lack of understanding how things really work. - Nicolas Bousquet
This question could do without the drama and comments about "fanboyism", "shaking somebody's beliefs" and the like. Esp. since the whole discussion boils down to "my definition of memory leak is better than yours". - LAFK says Reinstate Monica

5 Answers

45
votes

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.

19
votes

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.

9
votes

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".

8
votes

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.

3
votes

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.