Why is Go so low on memory consumption compared to Java?

3,921 views
Skip to first unread message

Haddock

unread,
Aug 21, 2015, 4:02:36 AM8/21/15
to golang-nuts
Hello,

I lately had a look at the Computer Language Benchmarks Game to see how Go is doing compared to other languages. What really surprises me is that Go is much better on memory consumption than other GC-based languages such as Java (see the column "Memory KB").

The various GCs in the JVM are really not that badly done. They belong to the best GCs that exist. So how comes Go has memory consumption that is multiple times lower than Java for many of the benchmarks in that Computer Language Benchmarks Game? Go is even doing quite well compared to C. Is that because Go has pointers whereas in Java and other GC-based languages everything is a reference instead? But a reference is also some pointer. It is just hidden to the developer that there is "a pointer behind the reference" (you know what I mean). Anyone has an idea why memory consumption in Go is that low?

Cheers, Haddock

Brad Fitzpatrick

unread,
Aug 21, 2015, 4:19:06 AM8/21/15
to Haddock, golang-nuts
Go also has value types and doesn't have to waste any memory JITing, since it's AOT compiled.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Egon

unread,
Aug 21, 2015, 4:28:34 AM8/21/15
to golang-nuts
Short version is that Go has structs and Java has classes.

Each class/object has an overhead whereas structs do not.
Go can embed structs, Java doesn't. This means that you always have an additional pointer when using another object/class.
Go also has slices and arrays for structs (non-primitives). You don't have to have a pointer per element in the array.

All of this results in less memory use and less memory fragmentation.

Of course that's not the full story; Java also pre-allocates more memory at the start for the GC and runtime, it also uses memory for JITing... etc. So there are runtime differences as well.

+ Egon

unread,
Aug 21, 2015, 1:58:05 PM8/21/15
to golang-nuts
On Friday, August 21, 2015 at 10:02:36 AM UTC+2, Haddock wrote:
Hello,

I lately had a look at the Computer Language Benchmarks Game to see how Go is doing compared to other languages. What really surprises me is that Go is much better on memory consumption than other GC-based languages such as Java (see the column "Memory KB").

The various GCs in the JVM are really not that badly done. They belong to the best GCs that exist.

In my opinion, reference counting GC combined with occasional cycle collection and with partial compile-time reference counting may be superior to JVM's GC in a non-negligible number of scenarios.
 
So how comes Go has memory consumption that is multiple times lower than Java for many of the benchmarks in that Computer Language Benchmarks Game?

Future prospects: The Java language made certain decisions with consequences affecting the GC implementation and memory footprint. For example, it is very hard to implement a compacting GC without increasing the JVM memory consumption because the Object class has the hashcode() method that must return the same hashcode irrespective of the object's memory address. Go is more compaction-friendly than Java in this respect - Go's map is a builtin type so in theory map[*K]V allows memory compaction without increasing memory consumption.

Go is even doing quite well compared to C. Is that because Go has pointers whereas in Java and other GC-based languages everything is a reference instead?

From the viewpoint of longer time spans, Go's memory consumption is better than Java's memory consumption because Java compilers haven't cracked this particular problem yet. There appear to be no clues indicating when compilers for managed languages will solve this problem.
Reply all
Reply to author
Forward
0 new messages