如何停止golang gc并手动触发它? [英] How to stop the golang gc and trigger it manually?

查看:132
本文介绍了如何停止golang gc并手动触发它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我支持使用golang编写的数据库上的大表联接.但是gc花费太多时间.我想关闭go gc并手动触发它.如何配置go build args?

Currently I'm supporting big table join on a database written in golang. But the gc costs too much time. I want to close the go gc and trigger it manually. How to config the go build args?

推荐答案

runtime 包含您需要的所有详细信息:

Package documentation of runtime contains all the details you need:

GOGC变量设置初始垃圾回收目标百分比.当新分配的数据与上一个收集后剩余的实时数据之比达到此百分比时,将触发收集.默认值为GOGC = 100.设置GOGC = off将完全禁用垃圾收集器.运行时/调试程序包的SetGCPercent函数允许在运行时更改此百分比.请参见 https://golang.org/pkg/runtime/debug/#SetGCPercent .

The GOGC variable sets the initial garbage collection target percentage. A collection is triggered when the ratio of freshly allocated data to live data remaining after the previous collection reaches this percentage. The default is GOGC=100. Setting GOGC=off disables the garbage collector entirely. The runtime/debug package's SetGCPercent function allows changing this percentage at run time. See https://golang.org/pkg/runtime/debug/#SetGCPercent.

因此,您可以将环境变量 GOGC 设置为百分比,该百分比是新分配的数据与上一次收集之后剩余的活动数据的比率.

So you may set the environment variable GOGC to a percent which is the ratio of freshly allocated data to live data remaining after the previous collection.

当上述数据比率达到 GOGC 的值时,将启动(垃圾)收集.初始设置来自 GOGC env变量,如果未设置,则为 100 .值 off 禁用垃圾收集.

When the above data ratio reaches the value of GOGC, a (garbage) collection is initiated. The initial setting is taken from the GOGC env variable, or 100 if the variable is not set. The value off disables garbage collection.

在运行时,您可以通过调用 debug.SetGCPercent() ,传递一个负值以将其禁用:

At runtime you can change the GOGC ratio by calling debug.SetGCPercent(), pass a negative value to disable it:

debug.SetGCPercent(-1)

您可以使用 runtime.GC() .

You may trigger a garbage collection "manually" with runtime.GC().

完全禁用GC可能不是您想要的.阅读 runtime 的完整软件包文档,在其中可以找到有关如何微调GC和如何跟踪GC运行的详细信息.分析它们并采取相应措施.

Completely disabling GC might not be what you want though. Read the complete package doc of runtime where you find details about how to fine-tune GC and how to trace GC runs. Analyze them and act accordingly.

还请注意, Go 1.7 现已发布,并带有改进的垃圾收集器:

Also note that Go 1.7 was released today with improved garbage collector:

由于垃圾收集器的加速和标准库中的优化,程序的运行速度应稍快一些.与Go 1.6相比,具有许多空闲goroutine的程序遇到的垃圾回收暂停要短得多.

Programs should run a bit faster due to speedups in the garbage collector and optimizations in the standard library. Programs with many idle goroutines will experience much shorter garbage collection pauses than in Go 1.6.

如果还没有,请先测试使用Go 1.7编译的应用程序,然后再采取进一步的操作.

If you haven't, first test your application compiled with Go 1.7 before taking any further action.

这篇关于如何停止golang gc并手动触发它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆