Compressing native executables using UPX
Ultimate Packer for eXecutables (UPX) 是一款压缩工具,可以减小可执行文件的体积。Quarkus 可以压缩生成的可执行应用程序,以减小其体积。当出现以下情况时,此类压缩非常有用:
Ultimate Packer for eXecutables (UPX) is a compression tool reducing the size of executables. Quarkus can compress the produced native executable to reduce its size. Such compression is interesting when:
-
building CLI tools, and you want to reduce the disk footprint,
-
building small container images.
请注意,UPX 压缩:
Note that UPX compression:
-
increases your build time, mainly if you use high-compression levels
-
increases the startup RSS usage of the application
System vs. Container
UPX 压缩需要:
The UPX compression requires:
-
the
upx
command to be available in the systemPATH
; -
or to have built the native executable using an in-container build.
如果您的路径中提供了 upx
命令,Quarkus 会使用它。否则,如果您使用容器内构建方式(使用 quarkus.native.container-build=true
)构建本机镜像,并且构建镜像提供了 upx
命令,Quarkus 会从容器内部压缩可执行文件。
If you have the upx
command available on your path, Quarkus uses it.
Otherwise, if you built the native image using an in-container build (using quarkus.native.container-build=true
) and if the builder image provides the upx
command, Quarkus compresses the executable from inside the container.
如果您不属于上述情况之一,则压缩会失败。
If you are not in one of these cases, the compression fails.
upx
能够使用与您的主机不同架构和操作系统的可执行文件进行压缩。例如,macOS 机器上的 upx
能够压缩 Linux 64 位可执行文件。
upx
can compress executables using a different architecture and OS than your host machine. For example, upx
on a macOS machine can compress a Linux 64-bits executables.
Configuring the UPX compression
然后,在应用程序配置中,配置所需的 compression level 启用压缩:
Then, in your application configuration, enable the compression by configuring the compression level you want:
quarkus.native.compression.level=5
如果未设置压缩级别,则停用压缩。压缩将在构建本机可执行文件后发生,并且会替换可执行文件。
If the compression level is not set, the compression is disabled. The compression will happen once the native executable is built and will replace the executable.
Compression level
压缩级别范围为 1 到 10:
The compression level goes from 1 to 10:
-
1
: faster compression -
9
: better compression -
10
: best compression (can be slow for big files)
Extra parameters
您可以使用 --brute
或 --ultra-brute
通过 quarkus.native.compression.additional-args
参数向 upx 传递其他参数。该值是逗号分隔的参数列表:
You can pass extra parameter to upx, such as --brute
or --ultra-brute
using the quarkus.native.compression.additional-args
parameter.
The value is a comma-separated list of arguments:
quarkus.native.compression.level=3
quarkus.native.compression.additional-args=--ultra-brute,-v
详尽的参数列表可在 the UPX documentation 中找到。
The exhaustive list of parameters can be found in the UPX documentation.