英文:
If the capacity of a slice has been modified by the unsafe.Pointer, can the rest memory reused by the garbage collection?
问题
请问您需要将以下内容翻译为中文吗?
"Think about this case:
s := make([]byte, 512, 1024)
(*reflect.SliceHeader)((unsafe.Pointer(&s))).Cap = 512
are the last 512 bytes memory can be collected by the GC?
Whether it is or not, Why?"
英文:
Think about this case:
s := make([]byte, 512, 1024)
(*reflect.SliceHeader)((unsafe.Pointer(&s))).Cap = 512
are the last 512 bytes memory can be collected by the GC?
Whether it is or not, Why?
答案1
得分: 1
据我所知,当前的垃圾收集器不会收集部分切片或字符串。对于以下情况也是如此:
s=s[:512:512] // 类似于你的示例,但在Go1.3中是惯用写法
s=s[128:] // 前128个元素不会被收集。
英文:
As far as I know, the current garbage collector will not collect partial slices or strings. The same is true for:
s=s[:512:512] // Like your example but idiomatically starting in Go1.3
s=s[128:] // first 128 elements are not collected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论