Shitaibin / shitaibin.github.io

博客:Go语言、区块链
http://lessisbetter.site
67 stars 21 forks source link

Go的map中删除子map,内存会自动释放吗? | 大彬 LIB #13

Open Shitaibin opened 5 years ago

Shitaibin commented 5 years ago

http://lessisbetter.site/2018/09/29/go-map-delete/

结论在Go中,map中存放map,上层map执行delete,子层map占用的内存会释放,无需手动先释放子map内存,再在上层map执行删除。 实验在C++中,如果使用了map包含map的数据结构,当要释放上层map的某一项时,需要手动释放对应的子map占用的内存,而在Go中,垃圾回收让内存管理变得如此简单。 做2个对比实验,实验1:普通的map,map保存到是int到int的映射,会执行dele

jimmyyem commented 3 years ago

感觉是出于节省内存的考虑,大类型/占内存的内容就直接free掉了