In Go, deleting an entry of a map of pointers causes memory leak?

前端 未结 2 967
悲&欢浪女
悲&欢浪女 2021-01-14 16:21

first timer here,

The first NOTE in SliceTricks suggests that there is a potential memory leak problem when cutting or deleting elements in a slice

2条回答
  •  深忆病人
    2021-01-14 17:04

    No, there will not be any memory leaks when deleting from a map.

    In case of slices, since a slice actually uses an underlying array, as long as the slice exists - even if it uses just one slot in that array - the pointer items inside the array can not get garbage collected.

    "A slice describes a piece of an array" which implies the array needs to be there for the slice to exist and can not get collected by GC; as long as some code is pointing at the slice.

提交回复
热议问题