Binary Size in Go Applications: How to Use go-size-analyzer
Zxilly
Posted on July 5, 2024
In the Go community, the size of application binaries is always a hot topic. In the pursuit of extreme performance, every byte counts. Go is known for its simplicity and efficiency, but as projects grow in size, so can the binaries. In this article, I will show you how to use go-size-analyzer
and how to interpret the results.
Installation
The project is hosted on GitHub. You can visit https://github.com/Zxilly/go-size-analyzer to read the full documentation.
go-size-analyzer
provides precompiled versions which you can download from GitHub Release. It is also available through some package managers:
-
Homebrew (MacOS/Linux):
brew install go-size-analyzer
-
Scoop (Windows):
scoop install go-size-analyzer
-
Go Build and Install:
go install github.com/Zxilly/go-size-analyzer/cmd/gsa@latest
The compiled version needs to download the required resource files from GitHub at runtime. These files are embedded in the precompiled versions.
Usage
Run gsa --version
to ensure go-size-analyzer
is installed correctly.
Locate the binary you want to analyze. Here, we choose the Linux x86_64 version of CockroachDB.
gsa --web cockroach-linux-amd64
Wait for the command line to display:
Use your browser to visit http://localhost:8080
, and you will see:
If this is your project, you can click on the package name on the left to see specific details. Is there a rarely used dependency taking up a lot of space? Has embed
included extra files?
A simpler way is to check the sections on the right. We can see that the .debug_*
segments take up a lot of space.
Using go tool link --help
to view the specific parameters of Go's linker, we find:
-s disable symbol table
-strictdups int
sanity check duplicate symbol contents during object file reading (1=warn 2=err).
-tmpdir directory
use directory for temporary files
-v print link trace
-w disable DWARF generation
This means using the -s -w
parameters can remove the .symtab
and .debug_*
segments, saving about 40M
of space in this binary.
To pass parameters to the linker during compilation, you can use the -ldflags
parameter. The complete command would be go build -ldflags="-s -w"
.
Summary
In this article, we introduced how to use the go-size-analyzer
tool to analyze the size of Go application binaries. This tool allows you to visualize the space occupied by different parts of your program and identify areas for optimization. We found that in many cases, the .debug_*
segments take up a considerable amount of space, and using the -s -w
parameters of the Go linker can significantly reduce the binary size.
If you find this tool helpful, consider visiting my GitHub repository at https://github.com/Zxilly/go-size-analyzer and giving it a star. Your support is very important to me, as it not only motivates me to continue improving this tool but also helps more people discover this useful resource.
Posted on July 5, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.