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. On Sun, Oct 9, 2022 at 2:29 PM wagner riffel <w...@104d.net> wrote: > > 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 > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABAC%3DhuQYcch3AOjhPKvQbwGacUd%3DjYgtTbE_u_dj0kFkwYd8w%40mail.gmail.com.