最新消息:yaf表单扩展中新增加了浮点数、日期和集合的校验。php yaf框架扩展实践三——表单

golang协程占用内存测试

Golang 307浏览 0评论

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

转载请注明:快乐编程 » golang协程占用内存测试

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址