Binary Size in Go Applications: How to Use Go-size-analyze

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. This tool allows you to visualize the space occupied by different parts of your program and identify areas for optimization.


This content originally appeared on HackerNoon and was authored by Zxilly

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.

\ Overview treemap

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:

\ CLI output

\ Use your browser to visit http://localhost:8080, and you will see:

\ Target treemap

\ 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?

\ Treemap detail

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


This content originally appeared on HackerNoon and was authored by Zxilly


Print Share Comment Cite Upload Translate Updates
APA

Zxilly | Sciencx (2024-07-25T07:19:54+00:00) Binary Size in Go Applications: How to Use Go-size-analyze. Retrieved from https://www.scien.cx/2024/07/25/binary-size-in-go-applications-how-to-use-go-size-analyze/

MLA
" » Binary Size in Go Applications: How to Use Go-size-analyze." Zxilly | Sciencx - Thursday July 25, 2024, https://www.scien.cx/2024/07/25/binary-size-in-go-applications-how-to-use-go-size-analyze/
HARVARD
Zxilly | Sciencx Thursday July 25, 2024 » Binary Size in Go Applications: How to Use Go-size-analyze., viewed ,<https://www.scien.cx/2024/07/25/binary-size-in-go-applications-how-to-use-go-size-analyze/>
VANCOUVER
Zxilly | Sciencx - » Binary Size in Go Applications: How to Use Go-size-analyze. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/25/binary-size-in-go-applications-how-to-use-go-size-analyze/
CHICAGO
" » Binary Size in Go Applications: How to Use Go-size-analyze." Zxilly | Sciencx - Accessed . https://www.scien.cx/2024/07/25/binary-size-in-go-applications-how-to-use-go-size-analyze/
IEEE
" » Binary Size in Go Applications: How to Use Go-size-analyze." Zxilly | Sciencx [Online]. Available: https://www.scien.cx/2024/07/25/binary-size-in-go-applications-how-to-use-go-size-analyze/. [Accessed: ]
rf:citation
» Binary Size in Go Applications: How to Use Go-size-analyze | Zxilly | Sciencx | https://www.scien.cx/2024/07/25/binary-size-in-go-applications-how-to-use-go-size-analyze/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.