Versão em Português

Gopher UPX

UPX

The upx (Ultimate Packer for eXecutables) is an executable compression tool that can significantly reduce the size of compiled binaries, including those generated by the Go language.

# quick recipe to download and use upx

cd /tmp
wget https://github.com/upx/upx/releases/download/v5.0.2/upx-5.0.2-amd64_linux.tar.xz
tar xf upx-5.0.2-amd64_linux.tar.xz
/tmp/upx-5.0.2-amd64_linux/upx -9 caminho-para-o-binario

Results with Go binary

When we compile the Go application without stripping, we get a binary with 8914107 bytes:

go install ./app

Applying upx to the binary without stripping, the size reduces to 4850156 bytes:

/tmp/upx-5.0.2-amd64_linux/upx -9 ~/go/bin/app

When we compile the Go application with stripping, the binary size is 6177060 bytes:

go install -ldflags='-s -w' ./app

Applying upx to the binary with stripping, the size reduces to 2263460 bytes:

/tmp/upx-5.0.2-amd64_linux/upx -9 ~/go/bin/app

See summary in the table below:

Configuration Size (bytes) Reduction (%)
Without strip 8914107 0%
With strip 6177060 30.7%
Without strip, with upx 4850156 45.6%
With strip, with upx 2263460 74.6%

Results with Docker Hub image

On Docker Hub, the image with strip (without upx), whose executable appears above with 6177060 bytes, was 2.49 MB.

Applying upx to the binary with strip, the Docker image size was reduced to 2.12 MB.

The final reduction in Docker image size was 14.8%.

Conclusion

UPX produced a good reduction in the Go binary size: ~ 45% without strip and ~ 63% relative to the binary with strip.

In the Docker Hub image, for the binary with strip, the UPX reduction was smaller (~ 14%), as the registry already provides efficient compression.