GO版本:go version go1.4.2 linux/amd64
测试环境:
[root@localhost mpro]# more /proc/cpuinfo | grep "model name" model name : Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz model name : Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz model name : Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz model name : Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz [root@localhost mpro]# [root@localhost mpro]# grep MemTotal /proc/meminfo MemTotal: 3868776 kB [root@localhost mpro]# getconf LONG_BIT 64 [root@localhost mpro]# more /etc/redhat-release CentOS Linux release 7.1.1503 (Core)
测试用例:
package main import ( "time" ) func main() { for i := 0; i < 200000; i++ { go func() { time.Sleep(5 * time.Second) }() } time.Sleep(10 * time.Second) }
执行程序前:
top - 18:53:19 up 19 min, 3 users, load average: 0.00, 0.04, 0.11 Tasks: 522 total, 1 running, 521 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.1 us, 0.2 sy, 0.0 ni, 99.6 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 3868776 total, 3327732 free, 235764 used, 305280 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 3397344 avail Mem
执行程序后:
top - 18:53:37 up 19 min, 3 users, load average: 0.00, 0.04, 0.11 Tasks: 524 total, 1 running, 523 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.2 us, 10.6 sy, 0.0 ni, 88.2 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 3868776 total, 2793732 free, 752512 used, 322532 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 2880016 avail Mem
总结:20万个协程占用了500M内存 50万KB
平均一个协程占用2.5KB
有疑问加站长微信联系(非本文作者)