简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM