Memory consumption memory with many larynx

I tried to check how Go would execute 100,000 goroutines. I wrote a simple program to create many routines that do nothing but print some ads. I limited the size of MaxStack to just 512 bytes. But I noticed that the size of the program does not decrease with this. It consumed about 460 MB of memory and, therefore, about 4 KB per goroutine. My question is: can we set the maximum stack size below the minimum stack size (which can be 4 KB) for goroutines. How to set the minimum stack size that Goroutine starts with? The following is an example of the code I used for the test:

package main import "fmt" import "time" import "runtime/debug" func main() { fmt.Printf("%v\n", debug.SetMaxStack(512)) var i int for i = 0; i < 100000; i++ { go func(x int) { for { time.Sleep(10 * time.Millisecond) //fmt.Printf("I am %v\n", x) } }(i) } fmt.Println("Done") time.Sleep(999999999999) } 
+6
source share
2 answers

The runtime / debug.SetMaxStack function only detects what indicates that the program is infinitely recursive and terminates it. http://golang.org/pkg/runtime/debug/#SetMaxStack

By setting it absurdly low, it does nothing for the minimum stack size and limits the maximum size only if your program crashes, if the size of the used stack exceeds the limit.

Technically, a crash occurs only when the stack needs to grow, so your program will die when the stack needs more than 8 KB (or 4 KB before transition 1.2).

The reason your program uses a minimum of 4KB * nGoroutines is because the stacks are page aligned, so there cannot be more than one stack on a VM page. Therefore, your program will use at least nGoroutines worth pages, and operating systems usually only measure and allocate memory in increments of page size.

The only way to change the initial (minimum) stack size is to change and recompile run run (and possibly the compiler too).

Go 1.3 will include continuous stacks, which are generally faster than split stacks in Go 1.2 and earlier, which may also lead to smaller initial stacks in the future.

+7
source

It is currently not possible to set a minimum stack size for goroutines.

Go 1.2 increased the minimum size from 4 KB to 8 KB

The docs say:

β€œIn Go 1.2, the minimum stack size when creating goroutine was dropped from 4 KB to 8 KB. Many programs ran into performance problems with the old size, which tended to introduce costly switching of the stack segment critical sections. The new number was determined by empirical testing.

But they keep saying:

"Update: An increased minimum stack size may cause programs with more goroutines to use more memory. There is no workaround, but future release plans include new stack management technology that should better solve the problem."

Thus, in the future you may have more luck.

See http://golang.org/doc/go1.2#stack_size for more details.

+8
source

All Articles