This is a creation in Article, where the information may have evolved or changed.
Recently saw the implementation of the Select in Go Runtime (the Select in Go Runtime) and found that when the SELECT statement was executed within a for loop, Each cycle needs to go through the malloc object to the free object in the underlying runtime, and I think that the frequent memory allocation and release costs are not small, at least the memory is not in a stable state. So, I actually tested the memory situation using Select to manipulate channel and not use Select to operate channel two cases.
The test process is to run the program for 3 minutes, each time the loop sleep 1 seconds, every 10 seconds to collect memory usage data. For a more intuitive feel, I used the Goplot tool to chart the captured memory data.
Using Select
Test code: Https://gist.github.com/skoo87/6727151#file-test_channel_select-go
Do not use Select
Test code: Https://gist.github.com/skoo87/6727151#file-test_channel-go
In the above two figure, the topmost blue line represents the size allocated from the system 总的堆内存
, the Yellow Line in the middle represents the 空闲的堆内存
size, and the bottom red line represents the 使用的堆内容
size.
In both cases, the total allocated heap memory is the same, basically no gap, two test programs are simple, basically consistent. The amount of heap memory used and the free heap memory must have this relationship. Both of these data show that the two cases are obviously different, when using Select, because of the constant allocation of new memory, so the use of heap memory all the way up, the corresponding free memory will gradually become less. However, when you do not use SELECT, but directly operate the channel, you can obviously feel that the memory allocation is very stable.
This test is not to prove how bad the select is, but only that the select will allocate memory frequently, this is just a simple use of two select, memory performance is very obvious. But I personally do not support to write a bunch of ugly code without a select, even a hidden code, such as a no channel timeout. However, we should avoid a large number of Execute SELECT statements in the program.
Finally, with the data, the data visualization, more able to visualize the phenomenon of visual display, play more happy.