How does Go allocate memory in make or new calls?

自闭症网瘾萝莉.ら 提交于 2019-12-23 12:27:15

问题


When I create a new slice or struct with a make or new call:

s := make([]int64, 10, 100)
o := new(MyStruct)

How much memory does Go allocate with a memory allocation system call? Does it pre-allocate memory, so that subsequent calls don't trigger new system calls?

I am asking this because I need to allocate memory frequently in my code. I am not sure if I need to implement a memory allocater myself, or if I can depend on Go to do the dirty work. And if Go does pre-allocate memory, can I customize the chunk size for allocation?

I write some experiment code in Go, and run the code under strace, but I do not understand what Go does with the mmap system calls:

mmap(0xc000000000, 65536, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xc000000000
munmap(0xc000000000, 65536)             = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7efdd1017000
mmap(0xc208000000, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xc208000000
mmap(0xc207ff0000, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xc207ff0000
mmap(0xc000000000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xc000000000
mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7efdd1007000
mmap(NULL, 1439992, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7efdd0ea7000

回答1:


You might suffer from the "premature optimization" syndrome here. The language spec says nothing about how exactly the memory allocated by make() will be allocated.

For instance, a conforming implementation of Go might perform thorough analyzis of the function which does make([]foo, ...) and prove that:

  1. The slice value returned does not escape the function's scope.
  2. append() is not called on this slice value.
  3. The slice value it not passed to any called function.

In this case, the slice's backing store might be allocated right on the stack.

Sure I'm exaggregating but again, the semantics of this are not declared in the language specification. And there are currently two mature Go implementations (that one originated from Google, dubbed gc, and gccgo) with more to come (llvmgo seems like having a good chances to live up), and they all have their peculiarities.

So please consider reading up on pprof and Go profiling in general, and do profile sample (but real) code.

Searching the mailing list for the words "profile", "profiling", "heap AND profile", "CPU AND profile" and "pprof" will provide lots of insight on it to you.

Consider also this and this.



来源:https://stackoverflow.com/questions/31553048/how-does-go-allocate-memory-in-make-or-new-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!