Go race detector

race_detector

线上程序发现处理速度变慢,内存占用变大,后面排查发现 build的时候指定了-race 参数,导致处理速度和内存变大。
正确使用方法是:在运行测试的时候指定-race,来检查是否存在并发写的情况。不能用于线上程序。

官方文档:

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.

参考链接

Golang: Concurrency is Hard; So What Can We Do About It?
SRE: Debugging: Simple Memory Leaks in Go
Does the Go race detector catch all data race bugs?
Race-free doesn’t mean deterministic