Binary size and startup

Two numbers decide whether the size of a binary matters: how often it is downloaded and how much memory the machine has. A Go hello world is about a hundred and thirty times the size of the C++ one, and it starts in the same time. This page measures both, because both claims are usually made without a measurement.

The bytes

The two programs print one line and do nothing else.

#include <iostream> int main() { std::cout << "hello\n"; }

package main import "fmt" func main() { fmt.Println("hello") }

g++ -std=c++23 -O2 -Wall -Wextra hello.cpp -o hello
go build -trimpath -o hello_go hello.go
ls -l hello hello_go

-rwxr-xr-x 1 agent agent 18504 Nov 18 03:04 hello
-rwxr-xr-x 1 agent agent 2392742 Nov 18 03:06 hello_go

Eighteen kilobytes against 2.3 megabytes. The C++ binary is small because it contains almost nothing: the iostream code it calls is in libstdc++.so.6, and the C library is shared as well. The Go binary contains its runtime, its scheduler, the garbage collector and the parts of the standard library the program imports, and it links nothing:

readelf -d hello | grep NEEDED
readelf -d hello_go

0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libc.musl-x86_64.so.1]

There is no dynamic section in this file.

Two flags remove most of what a Go binary does not need at run time, the symbol table and the DWARF debug information: -ldflags="-s -w". The C++ equivalent is strip or -s:

go build -trimpath -ldflags="-s -w" -o hello_go_stripped hello.go
g++ -std=c++23 -O2 -s hello.cpp -o hello_stripped
ls -l hello_stripped hello_go_stripped

-rwxr-xr-x 1 agent agent 14072 Nov 18 03:04 hello_stripped
-rwxr-xr-x 1 agent agent 1585314 Nov 18 03:06 hello_go_stripped

Still 1.6 megabytes, and the stripping cost the stack traces their line numbers, which is a bad trade for a program that logs errors. A program that imports encoding/json and sort is a little larger again, because those packages pull in reflection:

package main import ( "encoding/json" "fmt" "sort" ) func main() { values := []int{5, 3, 9, 1, 7} sort.Ints(values) data, _ := json.Marshal(values) fmt.Println(string(data)) }

go build -trimpath -o sortjson sortjson.go
go build -trimpath -ldflags="-s -w" -o sortjson_stripped sortjson.go
ls -l sortjson sortjson_stripped

-rwxr-xr-x 1 agent agent 2775817 Nov 18 03:06 sortjson
-rwxr-xr-x 1 agent agent 1855650 Nov 18 03:06 sortjson_stripped

Binary Bytes Needs at run time
C++ hello, -O2 18,504 libstdc++, libgcc, libc
C++ hello, stripped 14,072 libstdc++, libgcc, libc
Go hello 2,392,742 nothing
Go hello, -s -w 1,585,314 nothing
Go with encoding/json and sort 2,775,817 nothing
Go with encoding/json and sort, -s -w 1,855,650 nothing

The time to the first line

Fifty runs of each binary after five warm-up runs, timed by hyperfine. The number includes the fork and exec that the shell does, which is most of what either program costs:

hyperfine --warmup 5 --runs 50 './hello' './hello_stripped' './hello_go' './hello_go_stripped'

Benchmark 1: ./hello
Time (mean ± σ): 2.8 ms ± 0.7 ms [User: 1.6 ms, System: 1.2 ms]
Benchmark 2: ./hello_stripped
Time (mean ± σ): 2.8 ms ± 0.9 ms [User: 1.8 ms, System: 0.9 ms]
Benchmark 3: ./hello_go
Time (mean ± σ): 3.2 ms ± 1.1 ms [User: 0.7 ms, System: 2.6 ms]
Benchmark 4: ./hello_go_stripped
Time (mean ± σ): 3.9 ms ± 0.6 ms [User: 0.9 ms, System: 2.6 ms]

The difference between the fastest and the slowest is 1.1 milliseconds, and the spread within one binary is wider than that. At this resolution the two languages start in the same time: the Go runtime startup disappears next to the cost of creating a process, and the C++ program pays the dynamic linker for three libraries instead. The stripped Go binary looks slower than the full one, which is noise, not a rule.

When the difference matters

A server that starts once and runs for months does not care about 2 MB, and neither does a desktop application. A command line tool that a CI job downloads a thousand times a day cares, and so does a container image built on scratch, where the Go binary is the whole image. A program for an embedded board with 8 MB of flash cares a great deal, and there C++ or Rust is the better answer; Go needs at least a few megabytes before it can print anything.

The C++ binary is not free either: it depends on an ABI. A binary built against one version of libstdc++ may refuse to start with another, and that is the price of the 18 KB. The Go binary has no such dependency and runs on any Linux of the same architecture, which is why the release pages of Go projects can be a single file per platform. The lessons behind the flags are linker flags and binary size and startup time in Go. The next page measures raw speed.

The C++ binary was built with g++ 15.2.0 (Alpine 15.2.0) and the Go binaries with go 1.26.10; hyperfine 1.19.0 took the timings on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image