英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论