Lädt...

🔧 Binary Size in Go Applications: How to Use go-size-analyzer


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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.

Image description

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:

Image description

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

Image description

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?

Image description

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.

...

🔧 Binary Search: Binary Search, Efficient Algorithms, Advanced Applications


📈 29.39 Punkte
🔧 Programmierung

🔧 Decimal to binary,Binary to decimal,Fibonacci series without third variable


📈 22.17 Punkte
🔧 Programmierung

🔧 Count ways of creating Binary Array ending with 1 using Binary operators


📈 22.17 Punkte
🔧 Programmierung

📰 Revisiting Non-separable Binary Classification and its Applications in Anomaly Detection


📈 18.3 Punkte
🔧 AI Nachrichten

🕵️ libiberty Binary Use-After-Free denial of service


📈 15.13 Punkte
🕵️ Sicherheitslücken

🐧 You Think the Visual Studio Code binary you use is Open Source? Think again.


📈 15.13 Punkte
🐧 Linux Tipps

🕵️ libiberty Binary Use-After-Free Denial of Service


📈 15.13 Punkte
🕵️ Sicherheitslücken

📰 Our Brains Use Binary Logic, Say Neuroscientists


📈 15.13 Punkte
📰 IT Security

📰 Our Brains Use Binary Logic, Say Neuroscientists


📈 15.13 Punkte
📰 IT Security

🕵️ MindshaRE: Using Binary Ninja API to Detect Potential Use-After-Free Vulnerabilities


📈 15.13 Punkte
🕵️ Hacking

🔧 How to use Advanced Binary Scarch ?


📈 15.13 Punkte
🔧 Programmierung

📰 Why do Computers even use Binary?


📈 15.13 Punkte
🔧 AI Nachrichten

🐧 vcs prompt that's still being maintained and I can get a simple-to-use binary for


📈 15.13 Punkte
🐧 Linux Tipps

📰 UAFuzz - Binary-level Directed Fuzzing For Use-After-Free Vulnerabilities


📈 15.13 Punkte
📰 IT Security Nachrichten

🕵️ Binary Number System: What Is It and Why Do Computers Use It?


📈 15.13 Punkte
🕵️ Hacking