Skip to content

Instantly share code, notes, and snippets.

@ryandotsmith
Created November 27, 2012 16:06
Show Gist options
  • Save ryandotsmith/4155111 to your computer and use it in GitHub Desktop.
Save ryandotsmith/4155111 to your computer and use it in GitHub Desktop.
Memory Lower Bound: Go vs. C

Have you ever wondered about the least amount of memory a C or Go program will use on a 64bit Linux machine? Kr & I have, here is the results of our curiosity:

A simple C program:

void
main()
{
  for (;;) {
  }
}

A simple Go program (go1.0.3):

package main

func main() {
    for {
    }
}

Observe the memory usage:

$ ps -p 16604,16607 -o comm,vsize,rss

COMMAND            VSZ   RSS
go               34652   252
c                 4148   352

The ratio of virtual/resident is quite different. In fact, if we ulimit our shell with 8192KB of memory, we can't even start our Go process.

$ ulimit -v 8192
$ ./go
Killed
$ ./c
@dustin
Copy link

dustin commented Nov 27, 2012

Interesting that C had a larger working set.

@mdz
Copy link

mdz commented Nov 27, 2012

That's because it was dynamically linked. Try compiling the C version with -static for a fairer comparison, RSS is more like 4k.

@kr
Copy link

kr commented Nov 27, 2012

Wow, interesting to see the effects of -static on a C program that only uses the standard library.

~ $ make c
cc     c.c   -o c
~ $ ./c&
[1] 13
~ $ ps -o comm,vsize,rss
COMMAND            VSZ   RSS
bash             17840  1848
c                 3860   352
ps                6548   884
~ $ kill %1
~ $ rm c
[1]+  Terminated              ./c
~ $ make c CFLAGS=-static
cc -static    c.c   -o c
~ $ ./c&
[1] 23
~ $ ps -o comm,vsize,rss
COMMAND            VSZ   RSS
bash             17840  1864
c                  896   156
ps                6548   884

Ironic, but in retrospect unsurprising, that dynamic linking makes it bigger.

@kr
Copy link

kr commented Nov 27, 2012

only uses the standard library

Excuse me, I should have said "uses no libraries". ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment