容器下的 Go 应用程序优化

1. 内存对齐

结构体内字段,从大到小排列

减少内存占用

1)安装 fieldalignment 工具

复制代码
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest

2)分析并修复内存对齐

复制代码
fieldalignment -fix  ./...

/Users/shaowenchen/Code/app/config/config.go:136:14: struct with 32 pointer bytes could be 24
/Users/shaowenchen/Code/app/config/config.go:150:11: struct of size 96 could be 88
/Users/shaowenchen/Code/app/config/config.go:166:14: struct of size 152 could be 144
/Users/shaowenchen/Code/app/config/config.go:194:12: struct with 80 pointer bytes could be 72
/Users/shaowenchen/Code/app/config/config.go:209:12: struct with 56 pointer bytes could be 40
/Users/shaowenchen/Code/app/dao/gormx/gorm.go:12:13: struct with 16 pointer bytes could be 8
/Users/shaowenchen/Code/app/dao/gormx/entity/cluster.go:5:14: struct with 128 pointer bytes could be 104

3)查看 fieldalignment 进行的优化

优化之前,struct of size 96:

复制代码
type CORS struct {
	Enable           bool
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	AllowCredentials bool
	MaxAge           int
}

优化之后,struct of size 88:

复制代码
type CORS struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	MaxAge           int
	Enable           bool
	AllowCredentials bool
}

fieldalignment 会自动结构体中的字段进行排序,从大到小排列。

原因是 Go 编译器在编译阶段,出于对 CPU 访问效率的考虑,保证尽量一次原子读取就可以读取一个完整字段,采用了一定的内存对齐策略。但这一策略会导致内存中的结构体存在空洞,导致内存占用率增加。

2. 设置合理的 GOMAXPROCS

写一个程序,获取当前 CPU 核心数,查看当前 GOMAXPROCS 的值:

复制代码
func main() {
	cpu := runtime.NumCPU()
	fmt.Println("Current CPU COUNT =", cpu)
	maxProcs := runtime.GOMAXPROCS(-1)
	fmt.Println("Current GOMAXPROCS =", maxProcs)
}

Pod Request CPU 为 10,Limit CPU 为 20。

复制代码
Current CPU COUNT = 32
Current GOMAXPROCS = 32

GOMAXPROCS 的值为物理机的 CPU 核心数,而不是容器的 CPU 核心数。过大的 GOMAXPROCS 会导致严重的上下文切换,浪费 CPU,在容器环境下,Go 程序不能最优设置 GOMAXPROCS,需要根据容器的 CPU 核心数来设置。

1)第一种方式是,设置环境变量 GOMAXPROCS 可以指定最大的 P 的数量。

复制代码
export GOMAXPROCS=8

Current CPU COUNT = 32
Current GOMAXPROCS = 8

程序会自动从环境变量中读取 GOMAXPROCS 的值进行设置。

2)第二种方式是,使用 automaxprocs 包自动设置 GOMAXPROCS。

复制代码
import _ "go.uber.org/automaxprocs"
func main() {
  // ...
}

引入包之后,会自动执行 init 方法,获取 Pod 的 CPU Limit 值,设置 GOMAXPROCS。

复制代码
 ./default
maxprocs: Updating GOMAXPROCS=20: determined from CPU quota
Current CPU COUNT = 32
Current GOMAXPROCS = 20

如果同时使用两种,环境变量的优先级高于 automaxprocs 包。

复制代码
export GOMAXPROCS=8

maxprocs: Honoring GOMAXPROCS="8" as set in environment
Current CPU COUNT = 32
Current GOMAXPROCS = 8
相关推荐
Dragon Wu几秒前
React Native MMKV完整封装
前端·javascript·react native·react.js
前端(从入门到入土)2 分钟前
解决Webpack打包白屏报错问题l.a.browse is not a function
前端·javascript
Jul1en_2 分钟前
【Web自动化测试】Selenium常用函数+IDEA断言配置
前端·selenium·intellij-idea
Marshmallowc3 分钟前
从源码深度解析 React:Hook 如何在 Fiber 中存储?DOM Ref 如何绑定?
前端·react.js·前端框架·fiber
LOYURU3 分钟前
Centos7.6安装Go
开发语言·后端·golang
小二·3 分钟前
Go 语言系统编程与云原生开发实战(第1篇):从零搭建你的第一个 Go 服务 —— 理解 GOPATH、Modules 与现代 Go 工作流
开发语言·云原生·golang
Eiceblue3 分钟前
Vue文档编辑器(Word默认功能)示例
前端·vue.js·word
羊锦磊4 分钟前
AI 助手大模型---阿里云创建AI应用
运维·服务器·数据库
Mr Xu_8 分钟前
在 Vue 3 中集成 WangEditor 富文本编辑器:从基础到实战
前端·javascript·vue.js
No Silver Bullet8 分钟前
HarmonyOS NEXT开发进阶(二十三):多端原生App中通过WebView嵌套Web应用实现机制
前端·华为·harmonyos