<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zxilly</title>
    <description>The latest articles on DEV Community by Zxilly (@zxilly).</description>
    <link>https://dev.to/zxilly</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1328357%2Fe9577d2c-0c5d-4706-90a0-08471492a8ff.jpeg</url>
      <title>DEV Community: Zxilly</title>
      <link>https://dev.to/zxilly</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zxilly"/>
    <language>en</language>
    <item>
      <title>Binary Size in Go Applications: How to Use go-size-analyzer</title>
      <dc:creator>Zxilly</dc:creator>
      <pubDate>Fri, 05 Jul 2024 10:43:06 +0000</pubDate>
      <link>https://dev.to/zxilly/binary-size-in-go-applications-how-to-use-go-size-analyzer-hmn</link>
      <guid>https://dev.to/zxilly/binary-size-in-go-applications-how-to-use-go-size-analyzer-hmn</guid>
      <description>&lt;p&gt;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 &lt;a href="https://github.com/Zxilly/go-size-analyzer"&gt;&lt;code&gt;go-size-analyzer&lt;/code&gt;&lt;/a&gt; and how to interpret the results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9c12u71y3tt4gr2hxj21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9c12u71y3tt4gr2hxj21.png" alt="Image description" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;The project is hosted on GitHub. You can visit &lt;a href="https://github.com/Zxilly/go-size-analyzer"&gt;https://github.com/Zxilly/go-size-analyzer&lt;/a&gt; to read the full documentation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go-size-analyzer&lt;/code&gt; provides precompiled versions which you can download from &lt;a href="https://github.com/Zxilly/go-size-analyzer/releases"&gt;GitHub Release&lt;/a&gt;. It is also available through some package managers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Homebrew&lt;/strong&gt; (MacOS/Linux):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install go-size-analyzer
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Scoop&lt;/strong&gt; (Windows):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoop install go-size-analyzer
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Go Build and Install&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go install github.com/Zxilly/go-size-analyzer/cmd/gsa@latest
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;The compiled version needs to download the required resource files from GitHub at runtime. These files are embedded in the precompiled versions.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;gsa --version&lt;/code&gt; to ensure &lt;code&gt;go-size-analyzer&lt;/code&gt; is installed correctly.&lt;/p&gt;

&lt;p&gt;Locate the binary you want to analyze. Here, we choose the Linux x86_64 version of CockroachDB.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gsa --web cockroach-linux-amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait for the command line to display:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feg9jeuvfgrrd1xvmyzp7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feg9jeuvfgrrd1xvmyzp7.png" alt="Image description" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use your browser to visit &lt;code&gt;http://localhost:8080&lt;/code&gt;, and you will see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubp0fwms1yweqfg6zf9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubp0fwms1yweqfg6zf9u.png" alt="Image description" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;embed&lt;/code&gt; included extra files?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnftxnxf76ja3tm8yhld0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnftxnxf76ja3tm8yhld0.png" alt="Image description" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A simpler way is to check the sections on the right. We can see that the &lt;code&gt;.debug_*&lt;/code&gt; segments take up a lot of space.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;go tool link --help&lt;/code&gt; to view the specific parameters of Go's linker, we find:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  -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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means using the &lt;code&gt;-s -w&lt;/code&gt; parameters can remove the &lt;code&gt;.symtab&lt;/code&gt; and &lt;code&gt;.debug_*&lt;/code&gt; segments, saving about &lt;code&gt;40M&lt;/code&gt; of space in this binary.&lt;/p&gt;

&lt;p&gt;To pass parameters to the linker during compilation, you can use the &lt;code&gt;-ldflags&lt;/code&gt; parameter. The complete command would be &lt;code&gt;go build -ldflags="-s -w"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we introduced how to use the &lt;code&gt;go-size-analyzer&lt;/code&gt; 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 &lt;code&gt;.debug_*&lt;/code&gt; segments take up a considerable amount of space, and using the &lt;code&gt;-s -w&lt;/code&gt; parameters of the Go linker can significantly reduce the binary size.&lt;/p&gt;

&lt;p&gt;If you find this tool helpful, consider visiting my GitHub repository at &lt;a href="https://github.com/Zxilly/go-size-analyzer"&gt;https://github.com/Zxilly/go-size-analyzer&lt;/a&gt; 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.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>productivity</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
