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/d227d9f0-90e1-fb62-2925-9a4729036708%40104d.net.

Reply via email to