A Guide to the Go Garbage Collector
小结:
1、
stack allocation
he space is stored on the goroutine stack.
escape to the heap
the Go compiler cannot determine its lifetime
垃圾回收目标:堆空间大小始终低于目标的堆空间大小。
The GC's goal is to finish a collection cycle before the total heap size exceeds the target heap size.
内存限制
Memory limit
Until Go 1.19, GOGC was the sole parameter that could be used to modify the GC's behavior. While it works great as a way to set a trade-off, it doesn't take into account that available memory is finite. Consider what happens when there's a transient spike in the live heap size: because the GC will pick a total heap size proportional to that live heap size, GOGC must be configured such for the peak live heap size, even if in the usual case a higher GOGC value provides a better trade-off.
The visualization below demonstrates this transient heap spike situation.
1.19 支持设置运行时内存限制
OOM 内存溢出
If the example workload is running in a container with a bit over 60 MiB of memory available, then GOGC can't be increased beyond 100, even though the rest of the GC cycles have the available memory to make use of that extra memory. Furthermore, in some applications, these transient peaks can be rare and hard to predict, leading to occasional, unavoidable, and potentially costly out-of-memory conditions.
That's why in the 1.19 release, Go added support for setting a runtime memory limit. The memory limit may be configured either via the GOMEMLIMIT
environment variable which all Go programs recognize, or through the SetMemoryLimit
function available in the runtime/debug
package.
This memory limit sets a maximum on the total amount of memory that the Go runtime can use.
Note: the target heap size is just a target, and there are several reasons why the GC cycle might not finish right at that target. For one, a large enough heap allocation can simply exceed the target. However, other reasons appear in GC implementations that go beyond the GC model this guide has been using thus far. For some more detail, see the latency section, but the complete details may be found in the additional resources.
reducing GC frequency may also lead to latency improvements
https://go.dev/doc/gc-guide
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2019-08-30 tmp
2018-08-30 Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.
2018-08-30 formData.append("username", "Groucho"); input 文件大小
2018-08-30 t
2018-08-30 dom 显示 与否 的对 ecmascript 变量的 监听
2018-08-30 go html ecmascript
2018-08-30 Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..