Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are goroutines garbage collected together with their channels?

Imagine the following code:

func waitForOneOfTwoProcesses() {

    c := make(chan bool)
    go func() {
        time.Sleep(1 * time.Second)
        c<-true
    }()
    go func() {
        time.Sleep(2 * time.Second)
        c<-true
    }()
    <-c

}

Does this leak a channel and a goroutine or does Go recognize that c is gone and the goroutine can exit?

Would the answer be any different if the channel had a buffer size of 2?

like image 738
AndreKR Avatar asked Jun 29 '16 04:06

AndreKR


People also ask

What are Goroutines and how do they work?

A goroutine is a lightweight execution thread in the Go programming language and a function that executes concurrently with the rest of the program. Goroutines are incredibly cheap when compared to traditional threads as the overhead of creating a goroutine is very low.

Are Goroutines concurrent?

Golang provides goroutines to support concurrency in Go. A goroutine is a function that executes simultaneously with other goroutines in a program and are lightweight threads managed by Go.

What are Goroutines and channels?

A goroutine is a function that runs independently of the function that started it. Sometimes Go developers explain a goroutine as a function that runs as if it were on its own thread. Channel is a pipeline for sending and receiving data. Think of it as a socket that runs inside your program.

How does channels work in Go?

In Golang, or Go, channels are a means through which different goroutines communicate. Think of them as pipes through which you can connect with different concurrent goroutines. The communication is bidirectional by default, meaning that you can send and receive values from the same channel.


1 Answers

If the channel is unbuffered, then one of the anonymous functions will not return. The program leaks a goroutine and channel.

If the channel has a buffer size greater than or equal to one, then both of the anonymous functions will return. The resources used by the goroutines and channel will be reclaimed.

A buffer size of one is sufficient to prevent the leak. The function waitForOneOfTwoProcesses receives one of the values sent to c. The second value sent to c is buffered in the channel (which is collected by the GC).

Another way to ensure that the goroutines return is to use non-blocking send. Replace the c <- true lines with:

 select {
 case c <- true:
 default:
 }
like image 142
Bayta Darell Avatar answered Oct 14 '22 20:10

Bayta Darell