dslfq06464 2018-10-01 14:07 采纳率: 0%
浏览 109
已采纳

如何使Go程序使用更多的内存? 推荐吗?

I'm looking for option something similar to -Xmx in Java, that is to assign maximum runtime memory that my Go application can utilise. Was checking the runtime , but not entirely if that is the way to go.

I tried setting something like this with func SetMaxStack(), (likely very stupid)

debug.SetMaxStack(5000000000) // bytes
model.ExcelCreator()

The reason why I am looking to do this is because currently there is ample amount of RAM available but the application won't consume more than 4-6% , I might be wrong here but it could be forcing GC to happen much faster than needed leading to performance issue.

What I'm doing

Getting large dataset from RDBMS system , processing it to write out in excel.

Another reason why I am looking for such an option is to limit the maximum usage of RAM on the server where it will be ultimately deployed.

Any hints on this would greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongzhiqi0332 2018-10-01 14:24
    关注

    The current stable Go (1.10) has only a single knob which may be used to trade memory for lower CPU usage by the garbage collection the Go runtime performs. This knob is called GOGC, and its description reads

    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 basically setting it to 200 would supposedly double the amount of memory the Go runtime of your running process may use.

    Having said that I'd note that the Go runtime actually tries to adjust the behaviour of its garbage collector to the workload of your running program and the CPU processing power at hand. I mean, that normally there's nothing wrong with your program not consuming lots of RAM—if the collector happens to sweep the garbage fast enough without hampering the performance in a significant way, I see no reason to worry about: the Go's GC is one of the points of the most intense fine-tuning in the runtime, and works very good in fact.

    Hence you may try to take another route:

    • Profile memory allocations of your program. Analyze the profile and try to figure out where the hot spots are, and whether (and how) they can be optimized.

      You might start here and continue with the gazillion other intros to this stuff.

    • Optimize. Typically this amounts to making certain buffers reusable across different calls to the same function(s) consuming them, preallocating slices instead of growing them gradually, using sync.Pool where deemed useful etc.

      Such measures may actually increase the memory truly used (that is, by live objects—as opposed to garbage) but it may lower the pressure on the GC.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮