douang2297 2016-08-16 10:04
浏览 830

如何停止golang gc并手动触发它?

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?

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-08-16 10:07
    关注

    Package documentation of runtime contains all the details you need:

    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.

    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.

    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.

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

    debug.SetGCPercent(-1)
    

    You may trigger a garbage collection "manually" with runtime.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.

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

    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.

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

    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗