12 ms·
https://blog.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick/ https://blog.filippo.io/shrink-your-go-binaries-with-this-on... This is great articl
by varbhat 6y ago
https://blog.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick/ https://blog.filippo.io/shrink-your-go-binaries-with-this-on...
This is great article instructing how to shrink the Go Binaries.
Author is lead of cryptography and security team of Go Team at Google.
- distortedsignal 6y agoCompletely agree. I performed the steps in the article and reduced the output binary size from ~35 MB for raw go build to around 29 MB for go build with the ldflags all the way down to 8.3 MB for compressed with upx. Really impressive results.
- Karunamon 6y agoDoes UPX still trigger virus scanners, though? As much as I like the idea of tinier binaries, the chance that your user's endpoint security is going to freak out makes it not worth the hassle.
- alternatetwo 6y agoNever mind that, startup times are atrocious with such a huge upx packed binary ...
- distortedsignal 6y agoThat's an interesting point. I hadn't tested the startup times of the upx binary before, so I just did by running `time $binary_name --help`. For my binary, this should print a simple help message and quit. Big: 0.04s user 0.01s system 101% cpu 0.054 total Med: 0.04s user 0.01s system 99% cpu 0.048 total Sm : 0.14s user 0.02s system 99% cpu 0.163 total So you're right - it makes execution time take about 3x as long. However, in my case, 3x slower is a fine trade off to make.
- devonkim 6y agoUPX binaries throw off most endpoint security models and so we have a chicken and egg problem where its more common use is for malware authors than commercial software vendors. But once UPX binary signatures are in significantly wider use new models can be developed to better score legit binaries.
- distortedsignal 6y agoI don't think UPX triggers (my) virus scanner. The McAfee daemon on my work laptop barfs at the EICAR Test File (https://kc.mcafee.com/corporate/index?page=content&id=KB59742 https://kc.mcafee.com/corporate/index?page=content&id=KB5974...) but it ignores the UPX binary right next to it.
- readams 6y agoVirus scanners just treat UPX as a compressed file format and scan the binary inside.
- corty 6y agoSome scanners also treat UPX as generally malicious.
- kingnothing 6y ago35MB seems relatively small already. What do you get from an 8MB binary that you don't from one 4x that size?
- qmmmur 6y ago27MB. Where that is important is context specific
- pantalaimon 6y ago> 35MB seems relatively small already 35 MB is huge! Are there any assets inside the binary?
- cesarb 6y ago> 35 MB is huge! For context, 35MB is two and a half boxes of high-density floppy disks. There were whole operating systems, complete with applications, smaller than that.
- pantalaimon 6y agoWere? My initrd + vmlinuz is still smaller than that today on a stock Ubuntu kernel.
- perl4ever 6y agoI remember when a OO framework like MacApp would produce a minimal "hello world" program that took up hundreds of KB and that was considered almost too bloated to be usable. I can't remember exactly how much, but the better part of a floppy to do nothing was ridiculous.
- erik_seaberg 6y agoSeems like a culture clash between “why would you not use your distro’s shared libraries?” and “containerize all the things!”
- qiqitori 6y agoAren't Go binaries completely static? That sort of is a large part of an operating system (the libraries minus hardware-related libraries) -- Just add a kernel (tens of MB) and whatever userland stuff you need to get your hardware to work (which could be "nothing" in some cases) and you're ready to go.
- ed25519FUUU 6y agotl;dr for those wondering: strip debugging and then compress the binary with upx: go build -ldflags="-s -w" main.go upx --brute main Just tried that on one of my binaries and went from 12 mB down to 3.6 mB.
- jaytaylor 6y agoWhat about process startup time?
- distortedsignal 6y agoI tried to answer upthread (https://news.ycombinator.com/item?id=23912394 https://news.ycombinator.com/item?id=23912394), but the long and short of it is that it takes about 3x longer to start the program for my example (35 MB no compression, 27 MB after ldflags="-s -w", 8 MB after UPX) after UPX compression than before.
- ausjke 6y agothis is what I use, I do not use -s -w link flags these days, just run strip over the binary and it gets the job done. then upx to make the size further down.
- paulie_a 6y agoDoes the smaller binary execute faster?
- someguy101010 6y agoNo but the smaller the binary, the smaller the download size. Also it results in smaller docker containers, which is a popular way to ship go binaries.