For range loop variable passing by reference to go routine causes memory leak

225 views
Skip to first unread message

davy zhang

unread,
Oct 7, 2022, 11:56:36 PM10/7/22
to golang-nuts

Original post on stackoverflow:

https://stackoverflow.com/questions/73985794/for-range-loop-variable-passing-by-reference-to-go-routine-causes-memory-leak


Code for reproducing the problem: 

https://go.dev/play/p/7Xzx1Aauzhh

go version go1.19 darwin/amd64

go tool pprof http://localhost:6060/debug/pprof/heap

func1 is leaking

https://i.stack.imgur.com/LTXHn.png

Is there any reason for this leaking?


Thank you for any input


wagner riffel

unread,
Oct 8, 2022, 11:59:48 PM10/8/22
to davy zhang, golang-nuts
On 08/10/2022 22:56, davy zhang wrote:
> Original post on stackoverflow:
>
> https://stackoverflow.com/questions/73985794/for-range-loop-variable-passing-by-reference-to-go-routine-causes-memory-leak
>
>
> Code for reproducing the problem:
>
> https://go.dev/play/p/7Xzx1Aauzhh
>
> go version go1.19 darwin/amd64
>
> go tool pprof http://localhost:6060/debug/pprof/heap
>
> func1 is leaking
>
> https://i.stack.imgur.com/LTXHn.png <https://i.stack.imgur.com/LTXHn.png>
>
> Is there any reason for this leaking?
>
>
> Thank you for any input
>

I don't think it's related with for-range loops, both should "leak"
the same, I bet that's tMap, currently in Go maps do not shrink, even
if you delete keys. One way to reduce memory usage of a long-living
map is to at some threshold copy over all its elements into a new map,
and set the old to map to this copy, then the old map, if not
referenced elsewhere, will eventually be gc'ed and cleaned up.

-w

davy zhang

unread,
Oct 9, 2022, 12:05:18 AM10/9/22
to w...@104d.net, golang-nuts
Thank you so much for pointing this out. I didn't realize that map
won't shrink in go, that's pretty counter intuitive.

I also found the issue here:
https://github.com/golang/go/issues/20135

It looks like the right direction.
Reply all
Reply to author
Forward
0 new messages