问题
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:
- The slice value returned does not escape the function's scope.
append()
is not called on this slice value.- 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