Go二进制文件与C语言相比的大小。

huangapple go评论98阅读模式
英文:

Go binary size in comparison with c

问题

昨天,我只是想比较一下简单的 Golang 的 HelloWorld 应用和 C 语言的差异。Golang 的二进制文件大约为 2-3 MB(只有 fmt.Println),而等效的 C 代码只有大约 20 kb(printf)。然后,我使用 strace 检查了两个二进制文件的系统调用,发现它们之间没有太大的差异。你有什么想法,为什么相比之下,Golang 的二进制文件如此庞大?

英文:

Yesterday I just wanted to compare simple golang HelloWorld app with c, go binary was something like 2-3 MB (just fmt.Println) the equivalent c code, however, was just about 20 kb (printf). Then I checked system calls both binaries were doing, using strace; there was not a huge difference between both so do you have any idea why golang binary is so massive in comparison with c equivalent?

答案1

得分: 7

Go二进制文件默认情况下是静态链接的,并且还包含更多的信息,例如关于类型的信息,这使得反射、类型断言等操作成为可能。引用Go FAQ中的一段话:

gc工具链中的链接器默认创建静态链接的二进制文件。因此,所有的Go二进制文件都包含Go运行时,以及支持动态类型检查、反射甚至是panic时的堆栈跟踪所需的运行时类型信息。

在Linux上,使用gcc编译和静态链接的简单C程序“hello, world”大约为750 kB,其中包括printf的实现。
使用等效的Go程序并使用fmt.Printf的话,大小约为2.3 MB,但其中包含了更强大的运行时支持和类型信息。

英文:

Go binaries are, by default, statically linked, and also contain more information such as information about types that allows for reflection, type assertions, and so on. To quote the Go FAQ:

> The linker in the gc tool chain creates statically-linked binaries by
> default. All Go binaries therefore include the Go run-time, along with
> the run-time type information necessary to support dynamic type
> checks, reflection, and even panic-time stack traces.
>
> A simple C "hello, world" program compiled and linked statically using
> gcc on Linux is around 750 kB, including an implementation of printf.
> An equivalent Go program using fmt.Printf is around 2.3 MB, but that
> includes more powerful run-time support and type information.

huangapple
  • 本文由 发表于 2016年10月20日 04:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/40141143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定