douyan1453 2017-10-12 19:00
浏览 41

Golang中的Goroutine大小不会线性增加

For the following code:

const LOOPNUM int = 200000

func main() {
    z := make(chan int16)
    for i := 0; i < LOOPNUM; i++ {
        go test(z)
    }
}

func test(a chan<- int16) {
    a <- -1
}

I ran the code with LOOPNUM = 200k and 400k, and the memory usage is like the following:

goroutine size comparison

Does anyone know the reason of the sudden memory increment after I doubled my goroutines (and any solution to reduce memory usage)?

Thanks!

  • 写回答

1条回答 默认 最新

  • duanmeng3573 2017-10-12 19:17
    关注

    You aren't waiting for the goroutines to finish, so it's exiting before it has a change to do everything you tell it to. Change it to this:

    const LOOPNUM int = 200000
    var wg sync.WaitGroup
    func main() {
        wg = sync.WaitGroup{}
        wg.Add(LOOPNUM)
        z := make(chan int16)
        for i := 0; i < LOOPNUM; i++ {
            go test(z)
        }
        wg.Wait()
    }
    
    func test(a chan<- int16) {
        a <- -1
        wg.Done()
    }
    

    And you should get a better idea of what is happening.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭