问什么会挂掉。

package main

import ( "fmt" "sync" "runtime" )

func init() { runtime.GOMAXPROCS( runtime.NumCPU() ) } func main() {

var wg sync.WaitGroup

wg.Add(1000000)
for i:= 0; i < 1000000; i++ {

    go func(num int) {
        fmt.Println(1)
        wg.Done()
    }(i)

}

wg.Wait()
fmt.Println( "over ")

}

崩溃后的输出

goroutine 991060 [syscall]: syscall.Syscall(0x1, 0x1, 0xc4b2940588, 0x2, 0xc4aac9ce18, 0x4595e0, 0xc4b2968cc0) /usr/local/go/src/syscall/asm_linux_amd64.s:18 +0x5 syscall.write(0x1, 0xc4b2940588, 0x2, 0x8, 0xf75371fa0045cd99, 0xc4ac1dc398, 0xc282ffffffff) /usr/local/go/src/syscall/zsyscall_linux_amd64.go:1064 +0x55 syscall.Write(0x1, 0xc4b2940588, 0x2, 0x8, 0x45d48a, 0xc4ac1dc398, 0x4) /usr/local/go/src/syscall/syscall_unix.go:180 +0x49 os.(File).write(0xc42002e010, 0xc4b2940588, 0x2, 0x8, 0x0, 0xc4ac1dc210, 0xc4ac1dc238) /usr/local/go/src/os/file_unix.go:249 +0x78 os.(File).Write(0xc42002e010, 0xc4b2940588, 0x2, 0x8, 0x7f0919831e10, 0xc4aac9e4e0, 0x0) /usr/local/go/src/os/file.go:142 +0x5f fmt.Fprintln(0x4f6140, 0xc42002e010, 0xc4aac9cf90, 0x1, 0x1, 0x48a100, 0xc4ac9f5918, 0xc4aac9cf88) /usr/local/go/src/fmt/print.go:248 +0x8f fmt.Println(0xc4aac9cf90, 0x1, 0x1, 0x48a100, 0xc4ac9f5918, 0x0) /usr/local/go/src/fmt/print.go:257 +0x57 main.main.func1(0xc42000a2f0, 0xf1f2f) /home/gopath/src/T_Debug/Test.go:24 +0x93 created by main.main /home/gopath/src/T_Debug/Test.go:26 +0x92 exit status 2

已邀请:

voidint - https://github.com/voidint

赞同来自: andylau004

同时运行的goroutine太多了

changjixiong - 时常做白日梦的程序员

赞同来自: andylau004

巨量协程抢占stdout,然后就崩溃了,去掉fmt.Println(1)就能正常运行。

chenjpu

赞同来自: andylau004

package main

import ( "fmt" //"runtime" "sync" )

func main() {

var wg sync.WaitGroup

wg.Add(1000000)
for i := 0; i < 1000000; i++ {

    go func(num int) {
        fmt.Println(1)
        wg.Done()
    }(i)

}

wg.Wait()
fmt.Println("over ")

}

1.8 beta2 没挂 正常输出

andylau004

赞同来自:

可能 虚拟机内存分配的较少。

还有去掉 fmt.Println 会好些。。多谢大家回复

要回复问题请先登录注册