去内存消耗与许多goroutines [英] Go memory consumption with many goroutines

查看:317
本文介绍了去内存消耗与许多goroutines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查Go将如何与100,000个goroutine执行。我写了一个简单的程序来产生许多例程,只打印一些公告。我将MaxStack的大小限制为只有512字节。但我注意到的是程序的大小不会减少。它消耗大约460 MB的内存,因此每个goroutine大约4 KB。我的问题是,我们可以设置最大堆栈大小低于最小堆栈大小(可能是4 KB)goroutines。我们如何设置Goroutine开始的最小堆栈大小?
下面是用于测试的示例代码:

I was trying to check how Go will perform with 100,000 goroutines. I wrote a simple program to spawn that many routines which does nothing but print some announcements. I restricted the MaxStack size to just 512 bytes. But what I noticed was the program size doesn't decrease with that. It was consuming around 460 MB of memory and hence around 4 KB per goroutine. My question is, can we set max stack size lower than the "minimum" stack size (which may be 4 KB) for the goroutines. How can we set the minimum Stack size that Goroutine starts with ? Below is sample 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)
}


推荐答案

runtime / debug.SetMaxStack函数只确定一个点去考虑一个程序无限递归,并终止它。 http://golang.org/pkg/runtime/debug/#SetMaxStack

The runtime/debug.SetMaxStack function only determines a what point does go consider a program infinitely recursive, and terminate it. http://golang.org/pkg/runtime/debug/#SetMaxStack

设置它荒谬低对堆栈的最小大小没有什么,只有当任何堆栈的使用大小超过限制时,你的程序崩溃,限制最大大小。

Setting it absurdly low does nothing to the minimum size of stacks, and only limits the maximum size by virtue of your program crashing when any stack's in-use size exceeds the limit.

技术上,崩溃只发生在堆栈必须生长时,所以当堆栈需要超过8KB(或者在执行1.2之前为4KB)时,你的程序会死掉。

Technically the crash only happens when the stack must be grown, so your program will die when a stack needs more than 8KB (or 4KB prior to go 1.2).

你的程序使用最少4KB * nGoroutines的原因是栈是页面对齐的,所以VM页面上永远不能有多个堆栈。因此,你的程序将使用至少nGoroutines的页面,并且OSes通常只以页面大小的增量测量和分配内存。

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

改变开始)堆栈的大小是修改和重新编译go运行时(也可能是编译器)。

The only way to change the starting (minimum) size of a stack is to modify and recompile the go runtime (and possibly the compiler too).

Go 1.3将包含连续堆栈,堆栈在Go 1.2和更早,并且也可能导致较小的初始堆栈在未来。

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

这篇关于去内存消耗与许多goroutines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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