Home > Enterprise >  Will golang garbage collecting channel of pointers when it's dequeued from channel?
Will golang garbage collecting channel of pointers when it's dequeued from channel?

Time:09-23

If I have something like this:

requests := make(chan *RequestStruct, 1000 * 1000)
responses := make(chan *ResponseStruct, 1000 * 1000)

If the all requests and responses already dequeued/consumed, those pointers that previously was there would someday will be garbage collected right? (or it won't because nobody ever set it to nil on the channel?)

CodePudding user response:

Channels are implemented in runtime/chan.go. Reading from a buffered channel is implemented by the function chanrecv, which does the following:

qp := chanbuf(c, c.recvx)
typedmemmove(c.elemtype, ep, qp)
typedmemclr(c.elemtype, qp)

The element that is stored in the channel is cleared by the typedmemclr, and will therefore not prevent the pointed-at element from being garbage-collected.

  •  Tags:  
  • go
  • Related