电子商务网站建设选择服务器要考虑的因素有工业设计公共服务平台
- 作者: 五速梦信息网
- 时间: 2026年03月21日 11:24
当前位置: 首页 > news >正文
电子商务网站建设选择服务器要考虑的因素有,工业设计公共服务平台,淘宝客网站模板有哪些,看空间网站Golang 逃逸分析#xff08;Escape Analysis#xff09;理解与实践篇 文章目录 1.逃逸分析2.相关知识#xff08;栈、堆、GC分析#xff09;3.逃逸分析综合-实践 demo 逃逸分析#xff08;Escape Analysis#xff09;是编译器在编译期进行的一项优化技术#xff0c;是Gl…Golang 逃逸分析Escape Analysis理解与实践篇 文章目录 1.逃逸分析2.相关知识栈、堆、GC分析3.逃逸分析综合-实践 demo 逃逸分析Escape Analysis是编译器在编译期进行的一项优化技术是Glang非常重要的性能优化工具。其目的是判断某个变量是否会被函数外部引用或者超出其作用范围。 1.逃逸分析 如果变量仅在函数内部使用那么它可以安全地分配在栈上如果变量“逃逸”到函数外部例如返回给调用者或者传递给其他协程编译器会将其分配到堆上以保证其生命周期不会在栈帧结束时被销毁。 1.返回指针如果函数返回了局部变量的指针该变量就会逃逸到堆上。 2.闭包捕获变量闭包函数中捕获的外部变量也会导致变量逃逸。 3.接口类型的转换接口转换时如果具体类型需要被持久化存储那么它可能逃逸。 4.动态分配的内存例如使用 new 或者 make 创建的对象编译器可能会决定将它们分配在堆上。 5.使用 Goroutine 需要特别注意变量逃逸问题。因为 Goroutine 会并发执行某些变量可能在 Goroutine 中被引用导致它们逃逸到堆上。 Golang 提供了逃逸分析的工具编译时查看函数中哪些变量发生了逃逸 go build -gcflags-m2.相关知识栈、堆、GC分析 栈分配栈是 Go 中快速分配和释放内存的区域。栈上的变量在函数返回时自动销毁不需要额外的垃圾回收GC开销。 堆分配堆上的内存分配速度相对较慢且需要依赖 Go 的垃圾回收机制进行管理。频繁的堆分配会导致 GC 的频率增加从而影响性能。 GCGo 的垃圾回收器是三色标记清除算法每次垃圾回收会对堆上的所有对象进行追踪和标记回收不再使用的内存。 白色待回收白色的对象表示未被访问到的对象。在垃圾回收开始时所有的对象最初都被标记为白色。最终所有仍然是白色的对象将被认定为不可达的并在清除阶段被回收。灰色待处理灰色的对象表示已经被垃圾回收器访问到但其引用的对象还没有完全处理。灰色对象需要进一步追踪其引用的对象。黑色已处理黑色的对象表示已经被处理过它的引用对象也已经被追踪不会被再次检查。黑色对象是安全的表示它们依然在使用不会被回收。 启用 GC 配置 export GOGC50 # 设置 GOGC 为 50增加 GC 频率降低内存占用 export GODEBUGgctrace1 # GC 运行的详细信息包括 GC 触发的时机、暂停时间、以及每次回收时清理的内存量GC 测试 demo package mainimport (fmtruntimetime )func main() {// 启动一个 Goroutine持续分配内存触发 GCgo func() {for {_ make([]byte, 1020) // 每次分配 10MB 内存time.Sleep(100 * time.Millisecond)}}()// 打印内存使用情况和 GC 次数var m runtime.MemStatsfor i : 0; i 10; i {runtime.ReadMemStats(m)fmt.Printf(Alloc %v MiB, Sys %v MiB, NumGC %v\n, m.Alloc/1024/1024, m.Sys/1024/1024, m.NumGC)time.Sleep(1 * time.Second)} }3.逃逸分析综合-实践 demo package mainimport (fmtruntime )// 示例1返回局部变量的指针 func escapeToHeap() *int {a : 42return a // 逃逸到堆上因为返回了局部变量的指针 }// 示例2闭包捕获外部变量 func closureExample() func() int {x : 100return func() int {return x // x 逃逸到堆上闭包捕获了外部变量} }// 示例3接口转换导致逃逸 func interfaceExample() {var i interface{}i 42 // 逃逸到堆上因为 interface 可能会持有较大对象fmt.Println(i) }// 示例4动态分配内存 func dynamicAllocation() {p : new(int) // 逃逸到堆上使用 new 分配内存*p 42fmt.Println(*p) }// 示例5在栈上分配 func noEscape() {x : 42 // 没有逃逸x 在栈上分配fmt.Println(x) }// 示例6Goroutine 中的逃逸分析 func goroutineEscape() {x : 42go func() {fmt.Println(x) // x 逃逸到堆上因为被 Goroutine 使用}() }func main() {// 打印当前内存使用情况var m runtime.MemStatsruntime.ReadMemStats(m)fmt.Printf(Initial Alloc %v KB\n, m.Alloc/1024)// 测试逃逸分析的各个示例fmt.Println(Running escapeToHeap())escapeToHeap()fmt.Println(Running closureExample())closure : closureExample()fmt.Println(closure())fmt.Println(Running interfaceExample())interfaceExample()fmt.Println(Running dynamicAllocation())dynamicAllocation()fmt.Println(Running noEscape())noEscape()fmt.Println(Running goroutineEscape())goroutineEscape()// 打印最终内存使用情况runtime.ReadMemStats(m)fmt.Printf(Final Alloc %v KB\n, m.Alloc/1024) }编译-逃逸分析 [jnjn ~]$ go build -gcflags-m escape.go
command-line-arguments
./escape.go:9:6: can inline escapeToHeap ./escape.go:15:6: can inline closureExample ./escape.go:17:9: can inline closureExample.func1 ./escape.go:26:13: inlining call to fmt.Println ./escape.go:33:13: inlining call to fmt.Println ./escape.go:39:13: inlining call to fmt.Println ./escape.go:45:5: can inline goroutineEscape.func1 ./escape.go:46:14: inlining call to fmt.Println ./escape.go:54:12: inlining call to fmt.Printf ./escape.go:57:13: inlining call to fmt.Println ./escape.go:58:14: inlining call to escapeToHeap ./escape.go:60:13: inlining call to fmt.Println ./escape.go:61:27: inlining call to closureExample ./escape.go:17:9: can inline main.func1 ./escape.go:62:21: inlining call to main.func1 ./escape.go:62:13: inlining call to fmt.Println ./escape.go:64:13: inlining call to fmt.Println ./escape.go:67:13: inlining call to fmt.Println ./escape.go:70:13: inlining call to fmt.Println ./escape.go:73:13: inlining call to fmt.Println ./escape.go:78:12: inlining call to fmt.Printf ./escape.go:10:2: moved to heap: a ./escape.go:17:9: func literal escapes to heap ./escape.go:25:2: 42 escapes to heap ./escape.go:26:13: … argument does not escape ./escape.go:31:10: new(int) does not escape ./escape.go:33:13: … argument does not escape ./escape.go:33:14: *p escapes to heap ./escape.go:39:13: … argument does not escape ./escape.go:39:13: x escapes to heap ./escape.go:45:5: func literal escapes to heap ./escape.go:46:14: … argument does not escape ./escape.go:46:14: x escapes to heap ./escape.go:54:12: … argument does not escape ./escape.go:54:47: m.Alloc / 1024 escapes to heap ./escape.go:57:13: … argument does not escape ./escape.go:57:14: Running escapeToHeap() escapes to heap ./escape.go:60:13: … argument does not escape ./escape.go:60:14: Running closureExample() escapes to heap ./escape.go:61:27: func literal does not escape ./escape.go:62:13: … argument does not escape ./escape.go:62:21: ~R0 escapes to heap ./escape.go:64:13: … argument does not escape ./escape.go:64:14: Running interfaceExample() escapes to heap ./escape.go:67:13: … argument does not escape ./escape.go:67:14: Running dynamicAllocation() escapes to heap ./escape.go:70:13: … argument does not escape ./escape.go:70:14: Running noEscape() escapes to heap ./escape.go:73:13: … argument does not escape ./escape.go:73:14: Running goroutineEscape() escapes to heap ./escape.go:78:12: … argument does not escape ./escape.go:78:45: m.Alloc / 1024 escapes to heap [jnjn ~]\(run [jnjn ~]\) ./escape Initial Alloc 187 KB Running escapeToHeap() Running closureExample() 100 Running interfaceExample() 42 Running dynamicAllocation() 42 Running noEscape() 42 Running goroutineEscape() Final Alloc 190 KB [jnjn ~]$end 1.尽量避免将局部变量的指针返回给外部。 2.使用闭包时注意外部变量的捕获避免逃逸。 3.尽量减少接口类型和 Goroutine 导致的逃逸。
相关文章
-
电子商务网站建设需要哪些步骤wordpress 4.5 中文404
电子商务网站建设需要哪些步骤wordpress 4.5 中文404
- 技术栈
- 2026年03月21日
-
电子商务网站建设需求分析报告学做网站买什么样的书
电子商务网站建设需求分析报告学做网站买什么样的书
- 技术栈
- 2026年03月21日
-
电子商务网站建设网室内设计师网站十大网站
电子商务网站建设网室内设计师网站十大网站
- 技术栈
- 2026年03月21日
-
电子商务网站建设有什么认识建设企业和建筑企业
电子商务网站建设有什么认识建设企业和建筑企业
- 技术栈
- 2026年03月21日
-
电子商务网站建设与管理笔试html代码注释符号
电子商务网站建设与管理笔试html代码注释符号
- 技术栈
- 2026年03月21日
-
电子商务网站建设与管理第二版域名注册哪个网站便宜
电子商务网站建设与管理第二版域名注册哪个网站便宜
- 技术栈
- 2026年03月21日






