golang 协程占多大内存


当前第2页 返回上一页

测试环境:

1

2

3

4

5

6

7

8

9

10

11

12

[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)

测试用例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package main

  

import (

    "time"

)

  

func main() {

    for i := 0; i < 200000; i++ {

        go func() {

            time.Sleep(5 * time.Second)

        }()

    }

  

    time.Sleep(10 * time.Second)

}

执行程序前:

1

2

3

4

5

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

执行程序后:

1

2

3

4

5

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 协程占多大内存的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

golang rune几个字节

go - 切片

golang网络数据传输过程中的binary.read与unsafe.pointer指针强转分析

关于golang之排序使用

26 goroutine channel实现并发和并行(三)

聊聊golang的zap的sink

golang中方法的receiver为指针和不为指针有什么区别

golang 高效的原地数组去重

string,byte,rune

go module来了,企业私有代理你准备好了吗?

更多相关阅读请进入《golang》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...