使用`-race`标志构建Go程序是否会有性能损失?

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

Is there any performance penalty building go program using -race flag?

问题

嗨,我想知道在使用go build -race构建的Go程序在生产环境中运行是否会有性能损失。

英文:

Hi I was wondering if there any performance penalty running a go program in production built using

go build -race

答案1

得分: 7

你可以在这篇文章中了解有关Go竞争检测器的信息:https://go.dev/doc/articles/race_detector

引用该文章的最后一段:

运行时开销

竞争检测的成本因程序而异,但对于典型的程序而言,内存使用可能增加5-10倍,执行时间可能增加2-20倍。

目前,竞争检测器为每个defer和recover语句分配额外的8字节内存。这些额外的分配直到goroutine退出时才会被回收。这意味着,如果你有一个长时间运行的goroutine,定期发出defer和recover调用,程序的内存使用可能会无限增长。这些内存分配不会显示在runtime.ReadMemStats或runtime/pprof的输出中。

英文:

You can read about it in the article post that describes the go race detector at https://go.dev/doc/articles/race_detector

Quoting from that article (the last paragraph) :

> Runtime Overhead
> ====
> The cost of race detection varies by program, but for a typical program, memory usage may increase by 5-10x and execution time by
> 2-20x.
>
> The race detector currently allocates an extra 8 bytes per defer and
> recover statement. Those extra allocations are not recovered until the
> goroutine exits
. This means that if you have a long-running goroutine
> that is periodically issuing defer and recover calls, the program
> memory usage may grow without bound. These memory allocations will not
> show up in the output of runtime.ReadMemStats or runtime/pprof.

huangapple
  • 本文由 发表于 2022年9月2日 09:48:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/73577034.html
匿名

发表评论

匿名网友

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

确定