Never start a goroutine without knowing how it will stop | Dave Cheney
In Go, goroutines are cheap to create and efficient to schedule. The Go runtime has been written for programs with tens of thousands of goroutines as the norm, hundreds of thousands are not unexpected. But goroutines do have a finite cost in terms of memory footprint; you cannot create an infinite number of them. Every time you use the go keyword in your program to launch a goroutine, you must know how, and when, that goroutine will exit. If you don’t know the answer, that’s a potential memory leak. Consider this trivial code snippet: This code obtains a channel of int from somefunction and starts a goroutine to drain it. When will this goroutine exit? It will only exit when ch is closed. When will that occur? It’s hard to say, ch is returned by somefunction. So, depending on the state of somefunction, ch might never be closed, causing the goroutine to quietly leak. In your design, some goroutines may run until the program exits, for example a background goroutine watching a configurat
In Go, goroutines are cheap to create and efficient to schedule. The Go runtime has been written for programs with tens of thousands of goroutines as the norm, hundreds of thousands are not unexpected. But goroutines do have a finite cost in terms of memory footprint; you cannot create an infinite number of them. Every time you use the go keyword in your program to launch a goroutine, you must know how, and when, that goroutine will exit. If you don’t know the answer, that’s a potential memory leak. Consider this trivial code snippet: ch := somefunction() go func() { for range ch {
related reading
- Notes on structured concurrency, or: Go statement considered harmful - njs blogvorpus.org
- The Go Memory Model - The Go Programming Languagego.dev
- Memory Allocation in Go | Melatoninghiant3223.github.io
- Golang landminesgist.github.com
- Frequently Asked Questions (FAQ) - The Go Programming Languagego.dev
- A Guide to the Go Garbage Collector - The Go Programming Languagego.dev
- Avoiding high GC overhead with large heaps | Gopher Academy Blogblog.gopheracademy.com
- Goroutines - Concurrent Programming in Goprogramiz.com
- Data Race Detector - The Go Programming Languagego.dev
- Deep Dive in Go Channels | Michalis Zampetakismzampetakis.com
- Go at Google: Language Design in the Service of Software Engineering - The Go Programming Languagego.dev
- A half-hour to learn Rustfasterthanli.me