dongwei6457 2019-03-14 15:26
浏览 358
已采纳

为什么简单的Go应用会消耗大量内存

this is very simple app :

package main
import "fmt"
func main() {

    for i:= 0; i < 100000; i++ {
        go func (){
            fmt.Println("hello message.")
        }()
    }

    fmt.Scanln()
    fmt.Println("done")
}

after run application on windows, and look at the windows task manager I saw this state:

go app state

someone can say why?

  • 写回答

1条回答 默认 最新

  • drd94483 2019-03-14 15:49
    关注

    Launched goroutines run concurrently, independent of each other. It's the responsibility and duty of the goroutine scheduler to handle them.

    A goroutine is a lightweight thread: it costs a lot less than an OS thread, but still costs something. Initial stack for a new goroutine is a couple of KBs (around 8KB), and grows / shrinks as needed. See Goroutines 8kb and windows OS thread 1 mb.

    Given that you launch 100,000 goroutines without any synchronization, it may very well be that you will have 100,000 goroutines running before any of them finishes. Estimated memory requirement for that would be:

    100,000 * 8KB = 800 MB
    

    So your app using 884MB is pretty much in line with the estimation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?