如何最小化Go中的垃圾收集? [英] How to minimize the garbage collection in Go?

查看:109
本文介绍了如何最小化Go中的垃圾收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候你可能想要避免/最小化垃圾收集器,所以我想确定如何去做。



我认为下一个是正确:


  • 在函数的开头声明变量。
  • 使用array而不是slice 。
  • 避免垃圾是相对直截了当的。你需要了解分配的位置,看看你是否可以避免分配。



    首先,在函数的开头声明变量不会有帮助。编译器不知道其中的差别。然而,人们会知道它们之间的差异,它会让它们感到烦恼。

    使用一个数组而不是一个分片是可行的,但这是因为数组(除非被解除引用)在堆栈上。数组还有其他问题,例如它们是通过函数之间的值(复制)传递的。堆栈中的任何内容都是不垃圾,因为函数返回时它将被释放。任何可能转义该函数的指针或切片都放在垃圾回收器必须处理的堆上。



    您可以做的最好的事情就是避免分配。当您完成不需要的大量数据时,请重新使用它们。这是Go博客上分析教程中使用的方法。我建议阅读它。



    除了分析教程中的另一个例子:假设你有一个类型的分片[] int 命名为 xs 。你不断追加到 [] int ,直到你达到一个条件,然后你重置它,这样你就可以重新开始。如果您执行 xs = nil ,则您现在将片段的基础数组声明为要收集的垃圾。 Append将在下次使用它时重新分配xs。如果您改为 xs = xs [:0] ,您仍然重置它,但保留旧数组。



    大多数情况下,尽量避免创建垃圾是过早的优化。对于您的大部分代码,这并不重要。但是你可能偶尔会发现一个被称为很多次的函数,每次运行它都会分配很多次。或者你重新分配而不是重复使用的循环。我会一直等到你看到瓶颈时才会过度。

    Some times you could want to avoid/minimize the garbage collector, so I want to be sure about how to do it.

    I think that the next one is correct:

    • Declare variables at the beginning of the function.
    • To use array instead of slice.

    Any more?

    解决方案

    Avoiding garbage is relatively straight forward. You need to understand where the allocations are being made and see if you can avoid the allocation.

    First, declaring variables at the beginning of a function will NOT help. The compiler does not know the difference. However, human's will know the difference and it will annoy them.

    Use of an array instead of a slice will work, but that is because arrays (unless dereferenced) are put on the stack. Arrays have other issues such as the fact that they are passed by value (copied) between functions. Anything on the stack is "not garbage" since it will be freed when the function returns. Any pointer or slice that may escape the function is put on the heap which the garbage collector must deal with at some point.

    The best thing you can do is avoid allocation. When you are done with large bits of data which you don't need, reuse them. This is the method used in the profiling tutorial on the Go blog. I suggest reading it.

    Another example besides the one in the profiling tutorial: Lets say you have an slice of type []int named xs. You continually append to the []int until you reach a condition and then you reset it so you can start over. If you do xs = nil, you are now declaring the underlying array of the slice as garbage to be collected. Append will then reallocate xs the next time you use it. If instead you do xs = xs[:0], you are still resetting it but keeping the old array.

    For the most part, trying to avoid creating garbage is premature optimization. For most of your code it does not matter. But you may find every once in a while a function which is called a great many times that allocates a lot each time it is run. Or a loop where you reallocate instead of reusing. I would wait until you see the bottle neck before going overboard.

    这篇关于如何最小化Go中的垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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